成员函数bind_param()对非对象错误

时间:2013-04-04 12:39:30

标签: php function

我非常傻眼。问题是什么?我已经从http://www.php.net/manual/en/mysqli-stmt.bind-param.php复制了默认的PHP OOS并且它一直在抛出错误。

<?php
$mysqli = new mysqli('localhost', 'root', 'hidden', 'hidden');

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

$stmt = $mysqli->prepare("INSERT INTO users VALUES (?, ?)");
$stmt->bind_param('ss', $name, $password);

$name = "Test";
$password = "Test";

/* execute prepared statement */
$stmt->execute();

printf("%d Row inserted.\n", $stmt->affected_rows);

/* close statement and connection */
$stmt->close();

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

致命错误:在非对象中调用成员函数bind_param()   第11行的C:\ xampp \ htdocs \ cms \ index.php

1 个答案:

答案 0 :(得分:0)

更改这些行:(因为你将2个字符串参数插入到用户表中)

$stmt = $mysqli->prepare("INSERT INTO users VALUES (?, ?)");
$stmt->bind_param('sssd', $name, $password);

with :(我假设您必须插入名称和密码字段......如果没有,请使用适当的字段名称作为名称和密码)

if (!($stmt = $mysqli->prepare("INSERT INTO users(name,password) VALUES (?, ?)"))) {
    echo "Prepare failed: (" . $mysqli->errno . ") " . $mysqli->error;
}
$stmt->bind_param('ss', $name, $password);