php - 在准备好的语句中执行 SELECT 查询时遇到问题

标签 php mysqli prepared-statement

我遵循了一堆关于在准备好的语句中使用 SELECT 的不同示例,但没有返回任何内容。 编辑 我稍微更改了我的代码,如下所示:

$date1 = 2012-01-01;
$date2 = 2012-01-31;
$sql_con = new mysqli('db', 'username', 'password', 'database');

if($stmt = $sql_con->prepare("SELECT eventLogID FROM Country WHERE countryCode=? AND date BETWEEN ? AND ?")){

   $stmt->bind_param("sss", $country_code, $date1,$date2); 

    $stmt->execute();

  $i=0;
  while ($stmt->fetch()){
  $stmt->bind_result($row[$i]);
  $i++;
  }

  $stmt->close();
$sql_con->close();

现在所有需要的条目,除了第一个,都被添加到 $row[]。为什么不添加第一个条目? 提前致谢!

最佳答案

编辑 07/2015(自原始答案以来已对问题进行编辑,但基本原则相同)

永远不要 SELECT * 在生产环境中,它只会以奇怪的、不可预测的和看似无关的方式回来咬你一口。通过指定所需的列,您将确保列顺序、数据类型、约束和各种其他元素从长远来看不会给您带来问题。

这个答案仍然大部分有效,所以我会按原样保留在这里,但主要的收获是:使用 PDO,它可以完成 98% 的你需要的事情,而且更清晰、更简洁API 在同一个后端。如果您需要更复杂的特定于 RDBMS 的 API,那么您将已经了解您遇到的问题以及为什么需要 mysqli 等。


SELECT * 不适用于 MySQLi 准备好的语句。这是我推荐PDO的主要原因之一相反 - 以及将变量引用而不是值绑定(bind)到参数的荒谬要求。

$stmt->bind_result($row);

这不会将结果 绑定(bind)到一个变量,它只会绑定(bind)一个列。并且因为您使用了 SELECT *,所以它没有按照您的意愿执行。

如果您确实想在 PDO 上使用 MySQLi(正如我所说,我会推荐),可以在 this one 等评论中找到一些关于如何 SELECT * 的好例子。在 bind_result() 手册页上。

或者您可以只指定要检索的列:

$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
}

关于php - 在准备好的语句中执行 SELECT 查询时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11575432/

相关文章:

php - 如何使用 SilverStripe 显示文件存档

php - 通过选项卡更新补贴

php - 将字符串拆分两次并将其放入表中

php - 尝试将 "insert and update"查询转换为 mysqli

php - mysqli_fetch_assoc()需要参数/调用成员函数bind_param()错误。如何获取并修复实际的mysql错误?

php - 在 mysql 插入查询中阻止攻击性词、阻止链接等?

php - 使用 MySQL 和 mysqli 通过 IP 地址连接到 mysql

mysql - 使用MySQL准备好的语句并且仍然能够进行注入(inject)

php - mysqli_fetch_assoc()需要参数/调用成员函数bind_param()错误。如何获取并修复实际的mysql错误?

JavaPreparedStatement未设置值