在未定义的非对象和变量上调用成员函数 prepare()


Call to a member function prepare() on a non-object and variable undefined

我今天学习php非常糟糕。有人可以告诉我为什么这不起作用吗?我在第 23 行的非对象和未定义变量上调用成员函数(( 时出错这是代码:

<?php
// connect to the MySQL server
$conn = new mysqli('localhost', 'username', 'password', 'Org_db');
// check connection
 if (mysqli_connect_errno()) {
 exit('Connect failed: '. mysqli_connect_error());
 }
 //for insert user info into users table columns
 $id = '';
 $name = 'Bob Grundy';
  $pass = md5("secure");
  $email = 'bob@acgs.org';
  $reg_date = date("m/d/Y");
 //line 23
 if (!$stmt = $mysqli->prepare("INSERT INTO `users` (`id`, `name`, `pass`, `email`, 
 `reg_date`)     VALUES ( ?,?,?,?,?)")) {
echo "Prepare failed: (" . $mysqli->errno . ") " . $mysqli->error;
 }
 /* Prepared statement, bind and execute */

if (!$stmt->bind_param("issss", $id, $name, $pass, $email, $reg_date)) {
echo "Binding parameters failed: (" . $stmt->errno . ") " . $stmt->error;
}
if (!$stmt->execute()) {
echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
}
?>

您定义了$conn = new mysqli(..但是在使用!$stmt = $mysqli->prepare("..之后,在这里,$mysqli变量应更改为$conn例如:

 if (!$stmt = $conn->prepare("INSERT INTO `users` (`id`, `name`, `pass`, `email`, 
 `reg_date`)     VALUES ( ?,?,?,?,?)")) {
echo "Prepare failed: (" . $mysqli->errno . ") " . $mysqli->error;
 }