编辑 07/2015(问题自原始答案以来已被编辑,但基本原则相同)
永远不要 SELECT * 在生产环境中,它只会以奇怪、不可预测和看似无关的方式回来咬你。通过指定您想要的列,您将确保列顺序、数据类型、约束和各种其他元素从长远来看不会给您带来问题。
这个答案大部分仍然有效,所以我将其原样留在这里,但主要的收获是:使用 PDO,它可以完成 98% 的事情,而且更简洁、更简洁API 在同一个后端。如果您需要更复杂的特定于 RDBMS 的 API,那么您将已经了解您遇到的问题以及为什么需要 mysqli 等。
SELECT * 不适用于 MySQLi 准备好的语句。这是我推荐 PDO 的主要原因之一 - 以及将变量引用而不是值绑定到参数的荒谬要求。
$stmt->bind_result($row);
这不是将结果 row 绑定到变量,它只是绑定单个列。而且因为你使用了SELECT *,所以它并没有达到你想要的效果。
如果您确实想在 PDO 上使用 MySQLi(正如我所说,我会推荐),在 bind_result() 手册页上的 this one 等 cmets 中有一些很好的示例说明如何使用 SELECT *。
或者您可以只指定要检索的列:
$sql_con = new mysqli('db', 'username', 'password', 'database');
if($stmt = $sql_con->prepare("SELECT name, countryCode FROM Country WHERE countryCode = ?")) {
$stmt->bind_param("s", $country_code);
$stmt->execute();
$stmt->bind_result($name, $countryCode);
while ($stmt->fetch()) {
// Because $name and $countryCode are passed by reference, their value
// changes on every iteration to reflect the current row
echo "<pre>";
echo "name: $name\n";
echo "countryCode: $countryCode\n";
echo "</pre>";
}
$stmt->close();
EDIT 根据您的新代码,您应该这样做:
// $date1 will be int(2010), $date2 will be int(1980) because you didn't
// quote the strings!
//$date1 = 2012-01-01;
//$date2 = 2012-01-31;
// Connect to DB
$sql_con = new mysqli('db', 'username', 'password', 'database');
// Check for connection errors here!
// The query we want to execute
$sql = "
SELECT eventLogID
FROM Country
WHERE countryCode = ?
AND date BETWEEN ? AND ?
";
// Attempt to prepare the query
if ($stmt = $sql_con->prepare($sql)) {
// Pass the parameters
$date1 = '2012-01-01';
$date2 = '2012-01-31';
$stmt->bind_param("sss", $country_code, $date1, $date2);
// Execute the query
$stmt->execute();
if (!$stmt->errno) {
// Handle error here
}
// Pass a variable to hold the result
// Remember you are binding a *column*, not a row
$stmt->bind_result($eventLogID);
// Loop the results and fetch into an array
$logIds = array();
while ($stmt->fetch()) {
$logIds[] = $eventLogID;
}
// Tidy up
$stmt->close();
$sql_con->close();
// Do something with the results
print_r($logIds);
} else {
// Handle error here
}