在非对象和变量undefined上调用成员函数prepare()

时间:2013-10-13 05:28:27

标签: php

我有一个非常糟糕的学习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;
}
?>

1 个答案:

答案 0 :(得分:1)

您定义了$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;
 }