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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?