【问题标题】:check if array values exists in SQL or not检查 SQL 中是否存在数组值
【发布时间】:2022-09-30 23:12:11
【问题描述】:

为什么以下不起作用? 我想检查表中是否存在数组值。

$arr=range(1,1111);
// Attempt select query execution
$sql = \'SELECT * FROM tbl_numbers_stock WHERE lot_stock IN \' . implode( \',\', $arr ) . \' );\';
if($result = mysqli_query($link, $sql)){
    if(mysqli_num_rows($result) > 0){
        while($row = mysqli_fetch_array($result)){
            echo \"match found\";
        }

        // Close result set
        mysqli_free_result($result);
    } else{
        echo \"No records matching your query were found.\";
    }
} else{
    echo \"ERROR: Could not able to execute $sql. \" . mysqli_error($link);
}
 
// Close connection
mysqli_close($link);
?>

但它给出了错误

ERROR: Could not able to execute SELECT * FROM tbl_numbers_stock WHERE lot_stock IN 1,2,3,4,5,6,7,......1110,1111 );. You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near \'1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29...\' at line 1
  • IN 之后必须是(

标签: php sql


【解决方案1】:

您缺少左括号

$sql = 'SELECT * FROM tbl_numbers_stock WHERE lot_stock IN(' .内爆(',',$arr)。 ' );';

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-03-10
    • 1970-01-01
    • 2012-06-29
    • 2016-06-22
    • 2020-08-03
    • 1970-01-01
    相关资源
    最近更新 更多