Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have comma separated numbers in 1 database field (1,2,58,65) and want to check if $_SESSION['customer_id'] is in array, but don't know how to format $adminids to get it work. It works with 1 number, but not with more than 1. I already tried other methods than in_array, but I always fail.

This is my code.

$lsc_adminid_query = xtDBquery("SELECT lsc.option_id,
                            lsc.option_value 
                            FROM lsc_config lsc
                            WHERE lsc.option_id = 27");

while ($adminid_query = xtc_db_fetch_array($lsc_adminid_query)) {
    if (xtc_not_null($adminid_query['option_value'])) {
        $adminids = $adminid_query['option_value'];
    }
}
$lsc_cid = array($adminids);

if (in_array($_SESSION['customer_id'], $lsc_cid)) {
    $lsc_admin = "lsc_admin";
    if (!isset($_COOKIE[$lsc_admin])) {
        setcookie($lsc_admin, '1', time() + (7200), "/");
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
294 views
Welcome To Ask or Share your Answers For Others

1 Answer

Use FIND_IN_SET() to search for an element of a comma-separated list.

$lsc_adminid_query = xtDBquery("
    SELECT lsc.option_id
    FROM lsc_config lsc
    WHERE FIND_IN_SET({$_SESSION['customer_id']}, lsc.option_value)");

But it would be better to normalize your design so you don't have comma-separated lists in database columns in the first place.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...