MySQL query only returning COUNT












0















On my site I am executing an SQL query to fill information. 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:



Array ( [COUNT] => 2 ) 


I am completely failing to understand how it is returning a COUNT at all, let alone 2 rows. There isn't even 2 rows in the database. This happens for any username.










share|improve this question























  • If no connection is open, mysqli_real_escape_string() will return an empty string. It appears to me that you're connecting with db_connect() after using the string escape function. This could be buggering it up for you, but either way something should be changed. I recommend pdo if it works for your project scope.

    – George Appleton
    Jan 2 at 14:41











  • Also, worth using strict comparisons wherever you can ===. Also in PHP booleans are lowercase true, false, not True, False as those are aliased (but that's me being picky).

    – George Appleton
    Jan 2 at 14:43











  • Sorry, I should have included an example SQL string. The output is correct, without an empty escaped string. The query executes properly in Workbench and returns the correct values. The issue occurs for some reason between that step and the fetching of the values, as shown by the print_f outputs.

    – NebulaCoding
    Jan 2 at 14:46











  • Can you make it into a minimal example, so $sqlStr = "full string"; $out = ....; var_dump(mysqli_fetch_assoc($out));

    – George Appleton
    Jan 2 at 14:53













  • Sorry, let me check, you want the var_dump on the fetch, and the SQL string doing the query?

    – NebulaCoding
    Jan 2 at 14:56
















0















On my site I am executing an SQL query to fill information. 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:



Array ( [COUNT] => 2 ) 


I am completely failing to understand how it is returning a COUNT at all, let alone 2 rows. There isn't even 2 rows in the database. This happens for any username.










share|improve this question























  • If no connection is open, mysqli_real_escape_string() will return an empty string. It appears to me that you're connecting with db_connect() after using the string escape function. This could be buggering it up for you, but either way something should be changed. I recommend pdo if it works for your project scope.

    – George Appleton
    Jan 2 at 14:41











  • Also, worth using strict comparisons wherever you can ===. Also in PHP booleans are lowercase true, false, not True, False as those are aliased (but that's me being picky).

    – George Appleton
    Jan 2 at 14:43











  • Sorry, I should have included an example SQL string. The output is correct, without an empty escaped string. The query executes properly in Workbench and returns the correct values. The issue occurs for some reason between that step and the fetching of the values, as shown by the print_f outputs.

    – NebulaCoding
    Jan 2 at 14:46











  • Can you make it into a minimal example, so $sqlStr = "full string"; $out = ....; var_dump(mysqli_fetch_assoc($out));

    – George Appleton
    Jan 2 at 14:53













  • Sorry, let me check, you want the var_dump on the fetch, and the SQL string doing the query?

    – NebulaCoding
    Jan 2 at 14:56














0












0








0








On my site I am executing an SQL query to fill information. 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:



Array ( [COUNT] => 2 ) 


I am completely failing to understand how it is returning a COUNT at all, let alone 2 rows. There isn't even 2 rows in the database. This happens for any username.










share|improve this question














On my site I am executing an SQL query to fill information. 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:



Array ( [COUNT] => 2 ) 


I am completely failing to understand how it is returning a COUNT at all, let alone 2 rows. There isn't even 2 rows in the database. This happens for any username.







php mysql mysqli






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 2 at 14:32









NebulaCodingNebulaCoding

83




83













  • If no connection is open, mysqli_real_escape_string() will return an empty string. It appears to me that you're connecting with db_connect() after using the string escape function. This could be buggering it up for you, but either way something should be changed. I recommend pdo if it works for your project scope.

    – George Appleton
    Jan 2 at 14:41











  • Also, worth using strict comparisons wherever you can ===. Also in PHP booleans are lowercase true, false, not True, False as those are aliased (but that's me being picky).

    – George Appleton
    Jan 2 at 14:43











  • Sorry, I should have included an example SQL string. The output is correct, without an empty escaped string. The query executes properly in Workbench and returns the correct values. The issue occurs for some reason between that step and the fetching of the values, as shown by the print_f outputs.

    – NebulaCoding
    Jan 2 at 14:46











  • Can you make it into a minimal example, so $sqlStr = "full string"; $out = ....; var_dump(mysqli_fetch_assoc($out));

    – George Appleton
    Jan 2 at 14:53













  • Sorry, let me check, you want the var_dump on the fetch, and the SQL string doing the query?

    – NebulaCoding
    Jan 2 at 14:56



















  • If no connection is open, mysqli_real_escape_string() will return an empty string. It appears to me that you're connecting with db_connect() after using the string escape function. This could be buggering it up for you, but either way something should be changed. I recommend pdo if it works for your project scope.

    – George Appleton
    Jan 2 at 14:41











  • Also, worth using strict comparisons wherever you can ===. Also in PHP booleans are lowercase true, false, not True, False as those are aliased (but that's me being picky).

    – George Appleton
    Jan 2 at 14:43











  • Sorry, I should have included an example SQL string. The output is correct, without an empty escaped string. The query executes properly in Workbench and returns the correct values. The issue occurs for some reason between that step and the fetching of the values, as shown by the print_f outputs.

    – NebulaCoding
    Jan 2 at 14:46











  • Can you make it into a minimal example, so $sqlStr = "full string"; $out = ....; var_dump(mysqli_fetch_assoc($out));

    – George Appleton
    Jan 2 at 14:53













  • Sorry, let me check, you want the var_dump on the fetch, and the SQL string doing the query?

    – NebulaCoding
    Jan 2 at 14:56

















If no connection is open, mysqli_real_escape_string() will return an empty string. It appears to me that you're connecting with db_connect() after using the string escape function. This could be buggering it up for you, but either way something should be changed. I recommend pdo if it works for your project scope.

– George Appleton
Jan 2 at 14:41





If no connection is open, mysqli_real_escape_string() will return an empty string. It appears to me that you're connecting with db_connect() after using the string escape function. This could be buggering it up for you, but either way something should be changed. I recommend pdo if it works for your project scope.

– George Appleton
Jan 2 at 14:41













Also, worth using strict comparisons wherever you can ===. Also in PHP booleans are lowercase true, false, not True, False as those are aliased (but that's me being picky).

– George Appleton
Jan 2 at 14:43





Also, worth using strict comparisons wherever you can ===. Also in PHP booleans are lowercase true, false, not True, False as those are aliased (but that's me being picky).

– George Appleton
Jan 2 at 14:43













Sorry, I should have included an example SQL string. The output is correct, without an empty escaped string. The query executes properly in Workbench and returns the correct values. The issue occurs for some reason between that step and the fetching of the values, as shown by the print_f outputs.

– NebulaCoding
Jan 2 at 14:46





Sorry, I should have included an example SQL string. The output is correct, without an empty escaped string. The query executes properly in Workbench and returns the correct values. The issue occurs for some reason between that step and the fetching of the values, as shown by the print_f outputs.

– NebulaCoding
Jan 2 at 14:46













Can you make it into a minimal example, so $sqlStr = "full string"; $out = ....; var_dump(mysqli_fetch_assoc($out));

– George Appleton
Jan 2 at 14:53







Can you make it into a minimal example, so $sqlStr = "full string"; $out = ....; var_dump(mysqli_fetch_assoc($out));

– George Appleton
Jan 2 at 14:53















Sorry, let me check, you want the var_dump on the fetch, and the SQL string doing the query?

– NebulaCoding
Jan 2 at 14:56





Sorry, let me check, you want the var_dump on the fetch, and the SQL string doing the query?

– NebulaCoding
Jan 2 at 14:56












1 Answer
1






active

oldest

votes


















0














A workaround has been found. The output is iterated through with a while() loop beforehand. 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']. This properly fills the information. This fixes the issue, though I still don't understand what was causing the issue in the first place.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54008158%2fmysql-query-only-returning-count%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    A workaround has been found. The output is iterated through with a while() loop beforehand. 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']. This properly fills the information. This fixes the issue, though I still don't understand what was causing the issue in the first place.






    share|improve this answer




























      0














      A workaround has been found. The output is iterated through with a while() loop beforehand. 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']. This properly fills the information. This fixes the issue, though I still don't understand what was causing the issue in the first place.






      share|improve this answer


























        0












        0








        0







        A workaround has been found. The output is iterated through with a while() loop beforehand. 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']. This properly fills the information. This fixes the issue, though I still don't understand what was causing the issue in the first place.






        share|improve this answer













        A workaround has been found. The output is iterated through with a while() loop beforehand. 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']. This properly fills the information. This fixes the issue, though I still don't understand what was causing the issue in the first place.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 2 at 15:35









        NebulaCodingNebulaCoding

        83




        83
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54008158%2fmysql-query-only-returning-count%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            MongoDB - Not Authorized To Execute Command

            How to fix TextFormField cause rebuild widget in Flutter

            in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith