在浏览了几个小时的各种脚本示例并尝试了几乎所有我在寻找答案时可以远程理解的东西之后,我再次受到更聪明的人的摆布。我要做的就是从我的数据库用户表中返回一个名称。这有那么难吗,对我来说似乎是的。有人请让我直截了当。我已经对所有错误进行了排序,现在什么也没得到,我认为问题出在返回结果语句中。这是我的代码:
<?php
//connect to database
$mysqli = new mysqli('localhost', 'fiona', 'imB04', 'Org_db');
// check connection
if (mysqli_connect_errno()) {
echo "Connect failed: " . mysqli_connect_errno(); exit();
}
//Prepared statement, bind and execute
if (!$stmt = $mysqli->prepare("SELECT `name` FROM users WHERE name = ?" )) {
echo "Prepare failed: (" . $mysqli->errno . ") " . $mysqli->error;
}
//bind parametres
if (!$stmt->bind_param("i", $name)) {
echo "Binding param failed: (" . $stmt->errno . ") " . $stmt->error;
//execute query or return error
}
if (!$stmt->execute()) {
echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
}
//bind result variable
if (!$stmt->bind_result($name)) {
echo "bind result failed: (" . $stmt->errno . ") " . $stmt->error;
}
if ($result = $mysqli->prepare("SELECT `name` FROM users WHERE name = ?")) {
while ($row = $result->fetch_array())
{
echo $result['name'];
}
}
//Close statement
$stmt -> close();
//Close connection
$mysqli -> close();
?>