PHP / MySQLi上的错误绑定参数

时间:2016-01-07 12:37:21

标签: php mysqli

我正在尝试使用从上一页发布的数据创建一个insert语句,并将它们绑定到语句

我的SQL插入语句是这样的:

// insert new user
$db = createConnection();
$insertquery="insert into comments (com, userid, comdate, blogpost) values (?,?,?,?)";
$inst=$db->prepare($insertquery);
$inst->bind_param("sisi", $comment, $user, $today, $id);
$inst->execute();
$inst->close();

当我打开PHP调试时,这是我收到的错误

Fatal error: Call to a member function bind_param() on boolean in /blog/xcomment.php on line 29

为bind_param传入的数据是

Array ( [comment] => My New Comment [blogid] => 1 ) 2016-01-07 12:31:50

$user = $currentuser['userlevel'];
$comment = $_POST['comment'];
$id = $_POST['blogid'];
$today = date("Y-m-d H:i:s");

2 个答案:

答案 0 :(得分:1)

请尝试使用单引号:

$inst->bind_param('sisi', $comment, $user, $today, $id);

答案 1 :(得分:-1)

http://php.net/manual/en/mysqli-stmt.prepare.php

$ db-> prepare - return boolean。

它的正确用法:

<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

$city = "Amersfoort";

/* create a prepared statement */
$stmt =  $mysqli->stmt_init();
if ($stmt->prepare("SELECT District FROM City WHERE Name=?")) {

    /* bind parameters for markers */
    $stmt->bind_param("s", $city);

    /* execute query */
    $stmt->execute();

    /* bind result variables */
    $stmt->bind_result($district);

    /* fetch value */
    $stmt->fetch();

    printf("%s is in district %s\n", $city, $district);

    /* close statement */
    $stmt->close();
}

/* close connection */
$mysqli->close();
?>