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 谁会做这个啊#ensp#Boson NetSim
  • ¥15 如何编写针对TPS6503320FRGE型号的电源管理芯片的编程代码?
  • ¥15 设计简单目录管理系统,要满足以下内容
  • ¥15 关于九十度混合耦合器信号分析问题
  • ¥15 Cesm如何关闭不用的模块呢
  • ¥15 vue2两层数据导出为excle
  • ¥15 有人能帮我做一下这个项目吗
  • ¥15 网络上传速率比下载速率高
  • ¥15 (标签-游戏|关键词-网页游戏)
  • ¥15 这个咋搞🥹可能需要手把手教