Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
296 views
in Technique[技术] by (71.8m points)

mysql - PHP - How to format data for in_array

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

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

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...