简体   繁体   English

MySQL查询仅返回COUNT

[英]MySQL query only returning COUNT

On my site I am executing an SQL query to fill information. 在我的网站上,我正在执行SQL查询以填充信息。 The section of code doing so is this: 这样做的代码部分是这样的:

$user = mysqli_real_escape_string(db_connect(),$_SESSION['UserName']);
$sqlStr = "SELECT * FROM tbl WHERE username='{$user}'";
$out = mysqli_query(db_connect(), $sqlStr);

$settings = False;
if(!(0 == mysqli_num_rows($out))){
    $settings = True;
    $res = mysqli_fetch_assoc($out);
}

Which should only return one row for a given user. 对于给定的用户,它应该只返回一行。 However, the output is extremely funky. 但是,输出非常时髦。 For a given user who has an entry in the database, the output is as follows: 对于在数据库中具有条目的给定用户,输出如下:

mysqli_result Object ( [current_field] => 0 [field_count] => x [lengths] => Array ( [x] => x ) [num_rows] => 1 [type] => 0 )

This is correct, but then when mysqli_fetch_assoc is performed, the result is this: 这是正确的,但是然后当执行mysqli_fetch_assoc时,结果是这样的:

Array ( [COUNT] => 2 ) 

I am completely failing to understand how it is returning a COUNT at all, let alone 2 rows. 我完全无法理解它是如何返回COUNT的,更不用说2行了。 There isn't even 2 rows in the database. 数据库中甚至没有2行。 This happens for any username. 任何用户名都会发生这种情况。

A workaround has been found. 已找到解决方法。 The output is iterated through with a while() loop beforehand. 事先使用while()循环迭代输出。 The following is the working code: 以下是工作代码:

$settings = False;
If(!(0 == mysqli_num_rows($out))){
    $settings = True;
    while($res = mysqli_fetch_assoc($out)){
        $var = $res['var'];

        break;
    }
}

and then using $var throughout the site instead of $res['var']. 然后在整个站点中使用$ var而不是$ res ['var']。 This properly fills the information. 这样可以正确填充信息。 This fixes the issue, though I still don't understand what was causing the issue in the first place. 这可以解决问题,尽管我最初仍然不了解是什么导致了问题。

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

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