PHP和Mysqli代码错误消息
我有这个PHP代码,网站上的另一个用户对我很有帮助。它试图将数据库中的条目与用户在表单中提供的内容相匹配,并回显符合这些规范的图片 (我在前4行中定义了我的数据库信息,但本网站不想将它们写出来)PHP和Mysqli代码错误消息,php,mysqli,Php,Mysqli,我有这个PHP代码,网站上的另一个用户对我很有帮助。它试图将数据库中的条目与用户在表单中提供的内容相匹配,并回显符合这些规范的图片 (我在前4行中定义了我的数据库信息,但本网站不想将它们写出来) // Errors $error = array(); if (!isset($_POST['submit'])) $error[] = "The form was not set.<br />"; // Here we check if each of the variable
// Errors
$error = array();
if (!isset($_POST['submit']))
$error[] = "The form was not set.<br />";
// Here we check if each of the variable are set and have content
if (!isset($_POST['gender']) || strlen($_POST['gender']) == 0)
$error[] = "You must fill the gender field.<br />";
if (!isset($_POST['hair']) || strlen($_POST['hair']) == 0)
$error[] = "You must fill the hair field.<br />";
if (!isset($_POST['height']) || strlen($_POST['height']) == 0)
$error[] = "You must fill the height field.<br />";
if (!isset($_POST['body']) || strlen($_POST['body']) == 0)
$error[] = "You must fill the body field.<br />";
if (empty($error))
{
$con = mysqli_connect($db_host, $db_user, $db_pass, $db_name);
if ($con->connect_error)
die('Connect Error (' . mysqli_connect_errno() . ') '. mysqli_connect_error());
// Here we prepare your query and make sure it is alright
$sql = "SELECT * FROM `table` WHERE gender=? AND hair=? AND height=? AND body=?";
if (!$stmt = $con->prepare($sql))
die('Query failed: (' . $con->errno . ') ' . $con->error);
// Here we define the field types with 'ssss' which means string, string, string
// and also set the fields values
if (!$stmt->bind_param('ssss', $_POST['gender'], $_POST['hair'], $_POST['height'], $_POST['body']))
die('Binding parameters failed: (' . $stmt->errno . ') ' . $stmt->error);
// Now we finally execute the data to update it to the database
// and if it fails we will know
if (!$stmt->execute())
die('Execute failed: (' . $stmt->errno . ') ' . $stmt->error);
// Now we read the data
while ($row = $stmt->fetch_object())
{
$pic = $row->picture;
echo $row->picture, "\n";
}
$stmt->close();
$con->close();
}
else
{
echo "Following error occurred:<br />";
foreach ($error as $value)
{
echo $value;
}
}
?>
这是哪一行代码:
// Now we read the data
while ($row = $stmt->fetch_object())
无法直接从
mysqli\u stmt
对象获取结果。首先使用get_result
获取mysqli_result
对象,然后使用fetch_对象
:
$result = $stmt->get_result();
while ($row = $result->fetch_object())
请注意,只有在安装了mysqlnd的情况下,这才有效;如果没有,请参阅
bind\u result
和fetch
方法,以获得一种不太方便(但更兼容)的方法。MySQLi prepared语句无法调用fetch\u object()
,这可以通过query()
实现。您必须bind_result()
到变量中,然后调用fetch()
来填充这些变量。这可能是PDO普遍更受欢迎的主要原因。抓取是MySQLi准备语句的一大难题。可能与中的示例重复,请查看如何处理抓取。请注意,这取决于较新的mysqlnd驱动程序。感谢Robadob的编辑,他通知我有一个fetch\u object
方法;不幸的是,我用我的编辑打乱了他的编辑。
$result = $stmt->get_result();
while ($row = $result->fetch_object())