dongyuluan7494 2015-07-20 06:55
浏览 37

PHP:绑定数组值时准备语句的错误消息(MySQLi,SELECT)

I am new to PHP and MySQLi and hope someone can help me with this.

I would like to use a prepared statement for a Select query in order to prevent SQL injection - using MySQLi.

So far I have the below code which returns the following error:
"Call to a member function fetch_assoc() on a non-object...".

However, if I hardcode the $IN params (i.e. a list of integers) and remove the call_user_func_array line then it works as intended.

My PHP:

case "fetchContent":
    $content = $_POST["content"];
    $IN = implode(",", array_fill(0, count($content), "?"));

    $stmt = $conn->prepare("SELECT tID, en FROM TranslationsMain WHERE tID IN($IN) ORDER BY tID");
    call_user_func_array(array($stmt, "bind_param"), $content);
    $stmt->execute();
    $result = $stmt->get_result();
    while($arrTranslations = $result->fetch_assoc()){
        $translations[] = array("tID" => $arrTranslations["tID"], "content" => $arrTranslations["en"]);
    }
    var_dump($translations);
    break;

Note:
$IN creates something like the following - e.g. when $content contains 7 items then dumping $IN returns: string(13) "?,?,?,?,?,?,?"

Update - Example for $content:

array(7) {
  [0]=>
  string(1) "1"
  [1]=>
  string(1) "2"
  [2]=>
  string(1) "3"
  [3]=>
  string(1) "4"
  [4]=>
  string(1) "5"
  [5]=>
  string(1) "6"
  [6]=>
  string(1) "7"
}

Can someone tell me how to resolve this and maybe also provide an explanation why I am getting this error ?

Many thanks for any help,
Mike

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
    • ¥15 谁有desed数据集呀
    • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
    • ¥15 关于#hadoop#的问题
    • ¥15 (标签-Python|关键词-socket)
    • ¥15 keil里为什么main.c定义的函数在it.c调用不了
    • ¥50 切换TabTip键盘的输入法
    • ¥15 可否在不同线程中调用封装数据库操作的类
    • ¥15 微带串馈天线阵列每个阵元宽度计算
    • ¥15 keil的map文件中Image component sizes各项意思