Mysqli查询从DB返回一个简单的字符串行的空值

时间:2014-02-28 02:08:40

标签: javascript php jquery mysql mysqli

我正在尝试使用mysqli语句从MySQL表中检索单行。我已经尝试了几次不同的代码迭代,根据此论坛以及其他方面的各种先前问题巧妙地更改结构,但似乎除了'null'之外似乎没有得到任何结果。

这是一个更大的脚本的一部分,它通过带有jQuery的Ajax请求调用。我已经在下面包含了PHP和Javascript,虽然我对JS的确定相当自信(现在准备告诉别人......)。

关于我出错的地方的任何建议都会非常感激,因为我再也看不到树上的木头了,我只是绕圈子走了。

PHP:

//initiate new mysqli object
$retrieve_link = new AuctionMySQLi($db_host, $db_user, $db_password, $db_name); //custom subclass, this definitely works as is used in other scripts on the server

//prepares DB query. Query has been tested on phpmyadmin and returns the expected data set
$stmt = $retrieve_link->prepare("SELECT `item_number`,`item_name`,`item_category`,`end_date`,`auction_type`,`high_bid_number` FROM `item` WHERE `item_number`=2");

$stmt->execute(); //no params to bind, so execute straight away
$stmt->bind_result($item);
$stmt->fetch();
$dataset = $item->fetch_row();

$response[0] = $dataset; //returned data forms part of larger dataset
echo json_encode($response); //return the entire dataset to a jquery Ajax request
die;

JS:

//this definitely works as objects have been returned via the 'success' function as the code was being developed
$.ajax({
url         : "items/populate-home-page-script.php",
type        : "GET",
data        : {data:toSend},
dataType    : "json",
success     : function(data){
    alert(data[0]);
},
error       : function(jqXHR, textStatus, errorThrown){
    alert(textStatus+','+errorThrown);
}
});

return false;

我还尝试使用fetch_assoc()fetch_row()作为PHP查询的一部分,从PHP参考资料herehere开始。我还阅读了Stackoverflow thisthisthis中的这些问题,但我似乎仍然为我尝试的每个不同代码组合返回null

正如我在代码注释中所说的那样,我知道数据库的链接可以正常工作,因为我已经在其他脚本中使用过它,而且在这个脚本的其他区域 - 所以没有理由说这个对象不会工作要么。我也知道,当输入到phpmyadmin时,查询会返回预期的数据。

返回的数据只是一些字符串,我想要做的就是将大约16个返回的数据集存储到一个数组中,作为循环的一部分,然后将此数组返回给Ajax请求。

2 个答案:

答案 0 :(得分:1)

您正在使用“AuctionMySQLi”,它似乎扩展了常规的Mysqli驱动程序。我会假设它正确地做到了这一点。

你正在使用准备好的陈述,在这种情况下可能是一种过度杀伤力。你可以用这样的东西完成同样的事情(php 5.3,mysqli + mysqlnd):

$retrieve_link = new AuctionMySQLi($db_host, $db_user, $db_password, $db_name);
$result = $retrieve_link->query("SELECT `item_number`,`item_name`,`item_category`,`end_date`,`auction_type`,`high_bid_number` FROM `item` WHERE `item_number`=2");
if($result !== false) {
    echo json_encode($result->fetch_all());
} else {
    echo json_encode(array());
}
$retrieve_link->close();

如果您使用的是较旧的php版本,或者mysqlnd不可用,您也可以

$retrieve_link = new AuctionMySQLi($db_host, $db_user, $db_password, $db_name);
$result = $retrieve_link->query("SELECT `item_number`,`item_name`,`item_category`,`end_date`,`auction_type`,`high_bid_number` FROM `item` WHERE `item_number`=2");
if($result !== false) {
   $output = array();
    while($row = $result->fetch_assoc()) {
        $output[] = $row;
    }
    echo json_encode($output);
} else {
    echo json_encode(array());
}
$retrieve_link->close();

我也明白你想限制结果的数量。在这两种情况下,完成它的一个好方法是在SQL中使用LIMIT语句。这总体上降低了源头的开销。否则,您可以使用array_slice对解决方案1中的result-> fetch_all()或解决方案2中的$ output的输出进行切片。

最后,如果你坚持使用准备好的声明,请阅读说明 http://ca2.php.net/manual/en/mysqli-stmt.bind-result.php 并分析提供的例子:

$retrieve_link = new AuctionMySQLi($db_host, $db_user, $db_password, $db_name);
$stmt = $retrieve_link->prepare("SELECT `item_number`,`item_name`,`item_category`,`end_date`,`auction_type`,`high_bid_number` FROM `item` WHERE `item_number`=2");
$stmt->execute();
$stmt->bind_result($itemName, $itemCat, $endDate, $auctionType, $highBidder);
$output = array();
while($stmt->fetch()) {
    $output[] = array($itemName, $itemCat, $endDate, $auctionType, $highBidder);
}
echo json_encode($output);
$retrieve_link->close()

答案 1 :(得分:0)

在我看来,您可能会对->fetch()->fetch_row()感到困惑。你应该使用其中一个,但不能两个都使用。

尝试此操作来检索结果集:

$stmt->execute();
while ($dataset = $stmt->fetch_row()) {
    $response[] = $dataset; //returned data forms part of larger dataset
}

这会将结果集的每一行追加到$ response数组。

相关问题