duanhuanzhi6431 2013-03-17 05:18
浏览 73
已采纳

PHP和mysqli:使用预准备语句选择多个条件

I'm working on some prepared statements using mysqli in a php file with a database running on InnoDB. Most of the statements are working pretty well, but I have a select statement with multiple conditions that keeps returning a syntax error in my select statement, to be specific: near ? AND section_num = ? AND dept = ? AND semester = ? AND year = ? at line 1 as well as the following error:

Call to a member function bind_param() on a non-object.

Here's the snippet of code:

if (!$rs = $mysqli->query("SELECT id FROM courses WHERE course_num = ? AND section_num = ? AND dept = ? AND semester = ? AND year = ?")) {
        echo "Select Query Failed!: (" . $mysqli->errno . ") ". $mysqli->error;
    }
    if(!$rs->bind_param("ssssi", mysqli_real_escape_string($mysqli,$course_num), mysqli_real_escape_string($mysqli,$section_num),
        mysqli_real_escape_string($mysqli,$dept), mysqli_real_escape_string($mysqli,$semester), mysqli_real_escape_string($mysqli,$year))) {
        echo "Select Binding parameters failed: (" . $rs->errno .") " . $rs->error;
    }
    if (!$rs->execute()) {
        echo "Execute select failed: (" . $rs->errno . ") " . $rs->error;
    }

Any suggestions for how to form this statement to retrieve an id based on the 4 inputs would be great. Thanks!

  • 写回答

4条回答 默认 最新

  • dousi1961 2013-03-17 05:24
    关注

    You should be using prepare to prepare a statement, not query as that just executes a query.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line