Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 检查是否有指定数据的匹配记录库_Php_Mysql_Mysqli - Fatal编程技术网

Php 检查是否有指定数据的匹配记录库

Php 检查是否有指定数据的匹配记录库,php,mysql,mysqli,Php,Mysql,Mysqli,我有这个mysql语法 //select backup table and loop through all records until it find the row base from the id and then compare if the $itemname is match in itemname column or $itemdesc is match in itemdesc column $sql2="SELECT * FROM backup WHERE id='$id' AN

我有这个mysql语法

//select backup table and loop through all records until it find the row base from the id and then compare if the $itemname is match in itemname column or $itemdesc is match in itemdesc column
$sql2="SELECT * FROM backup WHERE id='$id' AND itemname='$itemname' OR itemdesc='$itemdesc'";
$result2 = mysqli_query($this->db,$sql2);
$user_data = mysqli_fetch_array($result2);
$count_row = $result2->num_rows;

if (!$count_row == 1) {
    //if theres no match either in the itemname or item desc..
    $error = $error + 1;
    echo "no match";
}

从上面可以看出,它将首先选择备份表并循环遍历其中的所有记录,直到它从id=$id中找到行基,然后比较$itemname在itemname列中是匹配的还是$itemdesc在itemdesc列中是匹配的,然后如果itemname或itemdesc中都不匹配,则生成并出错,但似乎不起作用,为什么????任何想法、线索、建议、建议、帮助都将不胜感激,谢谢

删除$user\u数据,因为它没有任何用处,并将sql语法更改为

$sql2="
    SELECT * FROM backup
    WHERE id='$id'
    AND itemname='$itemname'
    AND itemdesc='$itemdesc'
";

既然您想比较itemname或itemdesc。

您是否尝试过如果(!$count\u row>=1)?我会尝试吗?它会给我很多错误!我得到错误“尝试获取非对象的属性?????我是指这段代码!如果(!$count\u row>=1){//如果itemname或item desc中都不匹配,$error=$error+1;回显“不匹配”;}已解决,感谢您的帮助:)请注意,如果您的变量在这里包含用户输入,则此代码可能容易受到SQL注入的攻击。