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

时间:2015-03-29 11:43:04

标签: 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' 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中没有匹配则生成和错误但似乎不起作用,为什么????任何想法,线索,建议,建议,帮助将不胜感激,谢谢!

1 个答案:

答案 0 :(得分:0)

删除$ user_data,因为它没有用处并将您的sql语法更改为

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

因为您要比较itemname或itemdesc。