【问题标题】:check if theres a match record base from data specified检查是否有来自指定数据的匹配记录库
【发布时间】:2015-03-29 11:43:03
【问题描述】:

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

【问题讨论】:

  • 你试过 if (!$count_row >= 1) 吗?
  • 我会不会尝试,但它会在我脸上留下很多错误!
  • 我收到错误“试图获取非对象的属性
  • ?????我的意思是这段代码! if (!$count_row>= 1) { //如果 itemname 或 item desc 中没有匹配项.. $error = $error + 1; echo "不匹配"; }
  • 已解决,感谢您的帮助:)

标签: php mysql mysqli


【解决方案1】:

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

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

因为您想比较 itemname 或 itemdesc。

【讨论】:

  • 请注意,如果您的变量在此处携带用户输入,则此代码可能容易受到 SQL 注入的攻击。
猜你喜欢
  • 2014-06-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-28
  • 1970-01-01
  • 2013-08-07
  • 1970-01-01
相关资源
最近更新 更多