简体   繁体   English

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

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

I am trying to retrieve a single row from a MySQL table using a mysqli statement. 我正在尝试使用mysqli语句从MySQL表中检索一行。 I've tried several different iterations of code, subtly changing the structure based on various previous questions from this forum, and others, but can't seem to get any result other than 'null'. 我尝试了几次不同的代码迭代,根据该论坛上的各种先前问题以及其他一些问题巧妙地更改了结构,但似乎除了“ null”以外没有任何结果。

This is part of a larger script which is called via an Ajax request with jQuery. 这是较大的脚本的一部分,该脚本通过jQuery的Ajax请求进行调用。 I've included both the PHP and the Javascript below, though I'm fairly confident in the JS being OK (preparing to be told otherwise now...). 我已经在下面包括了PHP和Javascript,尽管我对JS可以正常运行非常有信心(准备现在告诉其他人...)。

Any suggestions as to where I'm going wrong would be very much appreciated as I can't see the wood from the trees anymore, and am just going around in circles. 由于我再也看不到树木上的木头了,所以关于我要去哪里的问题的任何建议将不胜感激。

PHP: 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: 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;

I have also tried using fetch_assoc() and fetch_row() as part of the PHP query, taking direction from the PHP reference material here and here . 我还尝试将fetch_assoc()fetch_row()作为PHP查询的一部分,从此处此处的PHP参考资料中获取指导​​。 I have also read through these questions from Stackoverflow this , this , and this , but I still seem to get a return of null for every different code combination I try. 我还从Stackoverflow thisthisthis中通读了这些问题,但是对于我尝试的每种不同代码组合,我似乎仍会返回null

As I've said in a code comment, I know that the link to the DB works as I've used it in other scripts, and in other areas in this script - so there's no reason why this object wouldn't work either. 就像我在代码注释中所说的那样,我知道到数据库的链接就像在其他脚本以及此脚本的其他区域中使用的那样起作用-因此,没有理由为什么该对象也不起作用。 I also know that the query returns the expected data when inputted to phpmyadmin. 我也知道查询输入到phpmyadmin后会返回预期的数据。

The returned data is just a number of strings, any all I would like to do is store around 16 returned datasets to an array, as part of a loop, and then return this array to the Ajax request. 返回的数据只是许多字符串,我想做的所有事情就是将大约16个返回的数据集存储到一个数组中,作为循环的一部分,然后将该数组返回给Ajax请求。

You are using "AuctionMySQLi" which appears to extend the regular Mysqli driver. 您正在使用“ AuctionMySQLi”,它似乎在扩展常规的Mysqli驱动程序。 I'll assume it does this correctly. 我假设它可以正确执行此操作。

You're using prepared statements which is probably an overkill in this case. 您正在使用准备好的语句,在这种情况下,这可能是过大的选择。 You could accomplish the same thing with something like this (php 5.3, mysqli + mysqlnd): 您可以使用以下代码(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();

If you're using an older php version, or mysqlnd is not available, you can also do 如果您使用的是较早的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();

I also understand that you want to limit the number of results. 我也了解您要限制结果数。 In both cases, a good way of getting it done is to use a LIMIT statement in SQL. 在这两种情况下,完成它的一个好方法是在SQL中使用LIMIT语句。 This is lower the overhead overall at source. 这样可以降低源头的总体开销。 Otherwise you can array_slice to slice the output of result->fetch_all() in solution 1, or $output in solution 2. 否则,您可以array_slice在解决方案1中对result-> fetch_all()的输出进行切片,或者在解决方案2中对$ output进行切片。

Finally, if you insist in using prepared statement read the note at http://ca2.php.net/manual/en/mysqli-stmt.bind-result.php and analyze provided example: 最后,如果您坚持使用准备好的语句,请阅读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()

It looks to me like you may have some confusion about ->fetch() and ->fetch_row() . 在我看来,您可能会对->fetch()->fetch_row()感到困惑。 You should use one or the other, but not both. 您应该使用其中一个,但不能同时使用两者。

Try this to retrieve your result set: 尝试此操作以检索结果集:

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

This will append each row of your result set to your $response array. 这会将结果集的每一行追加到$ response数组中。

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

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