我试图创建一个函数来检查在使用mysqli(全新的我)的mysql中的重复条目,但我不能让我的头绕着这个错误。我有3个PHP文件:db.php - db连接functions.php与我的功能(需要db.php)和submit.php - 接受输入的文件。致命的错误调用成员函数prepare()在非对象
db.php中:
<?php
define("HOST", "localhost"); // The host you want to connect to.
define("USER", "user"); // The database username.
define("PASSWORD", "pass"); // The database password.
define("DATABASE", "db_list"); // The database name.
$mysqli = new mysqli(HOST, USER, PASSWORD, DATABASE);
?>
的functions.php:
require 'db.php';
function check($str, $mysqli) {
$checkdupe = $mysqli->prepare("SELECT name FROM list WHERE str = ?"); //line giving me error
$checkdupe->bind_param("s", $str);
$checkdupe->execute();
$checkdupe->store_result();
if ($checkdupe->num_rows > 0) {
//dupe found
return true;
} else {
//no dupe
return false;
}
}
submit.php
require 'functions.php';
if (isset($_POST['name'])) {
if (check($_POST['name'], $mysqli) == true) { //added $mysqli parameter
echo "success!";
} else {
echo "fail;
}
} else {
echo 'invalid post';
}
?>
向我们展示如何调用check()函数。 –
这不是PDO ...这是mysqli,你应该有更好的错误处理。如果' - > prepare()'导致错误,那么'$ mysqli'不是一个有效的数据库连接句柄(周期,或者至少在脚本中的那个点,例如范围问题),并且可能是'connect'通话失败。 –
我们可以看到你的'submit.php'文件吗?这对确定错误的位置很有必要。 – Phas1c