doubi2014 2014-02-25 21:08
浏览 23
已采纳

准备好的陈述会导致错误

I am very new to prepared statements.

This is my code:

<?php
$getImages=$db->prepare("SELECT * FROM header_image_arabic");
$getImages->bind_param('s', '$images');
$getImages->execute();
$getImages->bind_result($returned_images);
while($img=$returned_images->fetch_object()){
?>

This is the error:

Fatal error: Cannot pass parameter 2 by reference in C:\wamp\www\arabia\admin\includes\pan\imageHeader.php on line 22`

What does this error mean?

  • 写回答

1条回答 默认 最新

  • doujiejujixi27244 2014-02-25 21:11
    关注

    There are number of things wrong here.

    '$images' is a string containing 7 characters. You want to pass the variable to bind_param, not a string.

    $getImages->bind_param('s', $images);
    

    Also, what exactly are you trying to bind to here? There are no placeholders (question marks) in your query. You can just use $db->query() here. You only need to "prepare" a query when you are filling in data.

    $getImages = $db->query('SELECT * FROM header_image_arabic');
    if($getImages === FALSE){
        die($db->error);
    }
    
    while($img = $getImages->fetch_object()){
    }
    

    Another thing is the line: $getImages->bind_result($returned_images);. bind_result is used to bind to the fields in the query. You can't use that here, since you are doing SELECT *. (Also, fetch_object and bind_result cannot be used together. You need to use get_result (which only works with the mysqlnd driver) to be able to use fetch_object.)

    So, if you wanted to use "prepared statements", it would look something like this:

    $getImages = $db->prepare("SELECT image_id FROM header_image_arabic WHERE name = ?");
    
    $getImages->bind_param('s', $images);
    $getImages->execute();
    $getImages->bind_result($returned_images);
    
    while($getImages->fetch()){
        // This will get updated each iteration
        echo $returned_images;
    }
    

    Or like this (requires mysqlnd driver):

    $getImages = $db->prepare("SELECT * FROM header_image_arabic WHERE name = ?");
    
    $getImages->bind_param('s', $images);
    $getImages->execute();
    $result = $getImages->get_result();
    
    while($img = $result->fetch_object()){
        echo $img->image_id;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥30 最小化遗憾贪心算法上界
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝