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 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教