繁体   English   中英

Mysqli查询正在从数据库返回简单字符串行的空值

[英]Mysqli query is returning a null value for a simple row of strings from a DB

我正在尝试使用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参考资料中获取指导​​。 我还从Stackoverflow thisthisthis中通读了这些问题,但是对于我尝试的每种不同代码组合,我似乎仍会返回null

就像我在代码注释中所说的那样,我知道到数据库的链接就像在其他脚本以及此脚本的其他区域中使用的那样起作用-因此,没有理由为什么该对象也不起作用。 我也知道查询输入到phpmyadmin后会返回预期的数据。

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

您正在使用“ 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()

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM