mysql没有正确选择数据


mySQLi is not SELECTing data appropriately

我正在从mysql_转换到mysqli。做一些教程。尽管阅读和谷歌搜索了这个主题,但收效甚微。我已经尝试了一个准备好的语句,但它一直输出0个结果(即。Now_rows =0)。当我手动查询mySQL表时,会返回许多结果。

mySQLI 5.0.96已启用

<?
include("conn.php");
// SELECT sql query
$sql = "SELECT id, rotation, assignedRad FROM sched_main WHERE thedate = ?"; 
// perform the query and store the result
$query = $conn->prepare($sql);
$thedate='2013-01-02';
$query->bind_param('s', $thedate);
$query->execute();
// if the $query contains at least one row
if ($query->num_rows > 0) {
  // output data of each row from $query
  while($row = $query->fetch_assoc()) {
    echo '<br /> id: '. $row['id']. ' - name: '. $row['rotation']. ' - pass: '.     $row['assignedRad'];
  }
  $query->free();
}
else {
  echo '0 results';
}
?>

问题2:有没有一种简单的方法来调试mysql ?使用mysql_query,我只需回显$sql;看看SELECT语句是什么样的,这是我调试过程的一部分。提前感谢

更新:下面是更新后的代码片段,如下所示:

$query->bind_param('s', $thedate);
if (!$query->execute()) {
  die($conn->error);
}
$query->store_result();
// if the $query contains at least one row
if ($query->num_rows > 0) {
  // output data of each row from $query
  while($row = $query->fetch()) {
    echo '<br /> id: '. $row['id']. ' - name: '. $row['rotation']. ' - pass: '. $row['assignedRad'];
  }
  $query->mysqli_free();
}
else {
  echo '0 results';
}

现在它输出数组$row值为无值:

id: - name: - pass: 
id: - name: - pass: 
id: - name: - pass:

. .等等…

我也得到mysqli_stmt::mysqli_free()

,当我尝试free()时,我得到:mysqli_stmt::free()

我不确定调试部分,但您可能需要在调用$query->num_rows之前存储DB执行调用的结果。此外,您可能想要检查准备和执行是否成功。

...
if (!$query = $conn->prepare($sql)) {
  die($conn->error);
}
$thedate='2013-01-02';
$query->bind_param('s', $thedate);
if (!$query->execute()) {
  die($conn->error);
}
$query->store_result();
if ($query->num_rows > 0) {
  ...
}

关于num_rowserror的更多信息请参阅PHP文档