duan19850312 2017-03-05 11:56
浏览 24

简单的PHP MySQL检查行是否存在返回null结果[重复]

This question already has an answer here:

I'm trying to build a simple postcode/ zip code check for our restaurant to check if a customer is within a list of postcodes we serve. Every time I query the postcode it returns a "not found" result and I can't work out why.

 $result = mysql_query("SELECT * FROM functions-postcodes WHERE postcode = 'PO111XX'");
if(mysql_num_rows($result) == 0) {
     echo "Postcode not within coverage area (postcode not found in table)";
} else {
     echo "Postcode IS within coverage area";
}

Here is what the database table looks like: Database screenshot

</div>
  • 写回答

1条回答 默认 最新

  • dreamer1231 2017-03-05 11:59
    关注

    Your table name should not have a - in it. Consider renaming the table to functions_postcode or something like that. If that is not possible, use backticks to escape special characters in (table) names:

    SELECT * FROM `functions-postcodes` ...
    

    That, and:

    1. Check for errors when you do not get the desired result (in logs or with error function calls)
    2. Stop using mysql_*. They are deprecated. Use MySQLi or PDO instead. Use prepared statements too.
    评论

报告相同问题?

悬赏问题

  • ¥15 Python报错怎么解决
  • ¥15 simulink如何调用DLL文件
  • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
  • ¥30 线性代数的问题,我真的忘了线代的知识了
  • ¥15 有谁能够把华为matebook e 高通骁龙850刷成安卓系统,或者安装安卓系统
  • ¥188 需要修改一个工具,懂得汇编的人来。
  • ¥15 livecharts wpf piechart 属性
  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了
  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题