0

我特别傻眼。问题是什么?我已经从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();
?>

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

4

1 回答 1

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);
于 2013-04-04T12:41:41.420 回答