dongou3158 2016-03-08 15:15
浏览 54

SQL语句上的php绑定参数不起作用

I'm trying to do a select on php but I'm having problems passing bind parameters to a standard sql statement. I already have lost all my day with this and I can't find any solution. I have a sql statement and a parameter (this part is changing constantly)

Here I have my sql statement and the execution:

$mysqli=getDB();
    $stmt = $mysqli->prepare('SELECT nom from noms where id=?'); // cambiar select 
    $sql->bind_param($id);
    $result = $mysqli->query($sql);
    $results = array();
    while($row = mysqli_fetch_array($result)){
        $results[] = array( // modificar el que passa dintre del bucle
            'nom' => $row['nom']
         );
    }

   echo json_encode($results);

Here is how I connect with database (getDB()):

function getDB() {
    // Credencials
    $servername = "server";
    $username = "user";
    $password = "pswwd";
    $dbname = "database";


    $mysqli = new mysqli($servername, $username, $password, $dbname);
    if ($mysqli->connect_errno) {
        echo "Error al connectar: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
    }

    return $mysqli;

}

All this is actually a web service function, when I call it I get an 500 message error. I need to run the statement changing the $id value (this parameters come from outside). I'm only receiving a 500 error status from the JS console but I don't see any error message.

Edit:

I did a few changes on the statement and execution:

$mysqli=getDB();
        $stmt = $mysqli->prepare('SELECT nom from noms where id=?'); // cambiar select 
        $stmt ->bind_param($id);
        $result = $mysqli->query($stmt );
        $results = array();
        while($row = mysqli_fetch_array($result)){
            $results[] = array( // modificar el que passa dintre del bucle
                'nom' => $row['nom']
             );
        }

       echo json_encode($results);

The log still showing the same message: PHP Fatal error: Call to a member function bind_param() on a non-object

  • 写回答

1条回答 默认 最新

  • dongpin1059 2016-03-08 15:19
    关注

    RTM: http://php.net/manual/en/mysqli-stmt.bind-param.php

    The syntax is

    $stmt->bind_param('types', $var1, $var2, etc....)
    

    You only provided the $var1 portion, and forgot the types. You need to tell mysqli what TYPE your parameter is, e.g.

    $stmt->bind_param('i', $id);
                      ^^^---missing in your version
    

    And note that ANYtime you get a 500, you immediately go look at your webserver's error log for actual details. What you see in your browser is deliberately vague, since 500 errors can reveal core/configuration details that should never become public.

    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵