2013-01-18 56 views
1

我正在从mysql_转换到mysqli。做一些教程。尽管阅读和搜索该主题,但没有取得多大成功。我尝试了一个准备好的语句,但它一直输出0个结果(即now_rows = 0)。当我手动查询mySQL表时,会返回一些结果。mySQLi没有正确选择数据

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:有没有一种简单的方法来调试mysqli的?使用mysql_query我只需要echo $ 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'; 
} 

现在,它与数组$行的值,因为没有值输出:

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

..等...

我也得到 致命错误:调用未定义的方法mysqli_stmt :: mysqli_free()

,当我尝试免费()我得到: 致命错误:调用未定义的方法mysqli_stmt :: free()的

回答

0

我不是确定调试部分,但在调用$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) { 
    ... 
} 

详情参见PHP的文档上num_rowserror