Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder stevenmw's Avatar
    Join Date
    Jun 2007
    Location
    OK
    Posts
    497
    Thanks
    27
    Thanked 31 Times in 31 Posts

    Mysqli API If Statement Works In One Place but Not Another

    I've been playing around with the mysqli API to get used to it. I'm trying to write an if statement where if there are more than 0 rows in a database a while loop is executed. However, I can't seem to get it to work.

    This code works:
    PHP Code:
    <?php
    $mysqli 
    = new mysqli("host""user""pass""db");


    if (
    mysqli_connect_errno()) {
        
    printf("Connect failed: %s\n"mysqli_connect_error());
        exit();
    }

    $query "SELECT * FROM test_table ORDER by id ASC";

    $ex $mysqli->query($query) or die(mysqli_error());

    $row_cnt mysqli_num_rows($ex);

    if (
    $row_cnt 0) { 
    echo 
    "hello <br />";
    }

    if (
    $result $mysqli->query($query)) {

        
    /* fetch associative array */
        
    while ($row $result->fetch_assoc()) {
            
    printf ("%s %s <br />"$row["id"], $row["title"]);
        }


        
    $result->free();
    }


    $mysqli->close($mysqli);
    ?>


    This if statements works:

    PHP Code:
    if ($row_cnt 0) { 
    echo 
    "hello <br />";

    If there are more than 0 rows the word "hello" is displayed followed by a line break.

    This if works:
    PHP Code:
    if ($result $mysqli->query($query)) {

        
    /* fetch associative array */
        
    while ($row $result->fetch_assoc()) {
            
    printf ("%s %s <br />"$row["id"], $row["title"]);
        }


        
    $result->free();

    This if statement does not work:
    PHP Code:
    if ($row_cnt 0)) {

        
    /* fetch associative array */
        
    while ($row $result->fetch_assoc()) {
            
    printf ("%s %s <br />"$row["id"], $row["title"]);
        }


        
    $result->free();

    Why does the if condition work in the earlier statement, but not this one?
    Last edited by stevenmw; 03-09-2013 at 10:54 AM.
    Thanks!

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    That depends entirely on what you've done and where this is. That last block has no method of determining if the 'if' branch is entered or if the while loop doesn't iterate.
    If you literally have this:
    PHP Code:

    $ex 
    $mysqli->query($query) or die(mysqli_error());

    $row_cnt mysqli_num_rows($ex);

    if (
    $row_cnt 0) { 
    echo 
    "hello <br />";
    }


    if (
    $result $mysqli->query($query)) {

        
    /* fetch associative array */
        
    while ($row $result->fetch_assoc()) {
            
    printf ("%s %s <br />"$row["id"], $row["title"]);
        }


        
    $result->free();
    }  

    if (
    $row_cnt 0) {

        
    /* fetch associative array */
        
    while ($row $result->fetch_assoc()) {
            
    printf ("%s %s <br />"$row["id"], $row["title"]);
        }


        
    $result->free();

    Than there is no problem with your if. It's the iteration and the result set that's no good.
    The $result has been freed. As soon as you do this, there is no way to iterate it again.
    Even without the free call, you still can't iterate it in the last if block. The problem is that its already on the null for the last record, so attempting to fetch will produce no results. You need to use mysqli_data_seek in order to move it back the the 0th record to start again.

    Edit:
    I just noticed though that you do have a syntax error on the last if branch. Not sure if that's in real code or not.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #3
    Regular Coder stevenmw's Avatar
    Join Date
    Jun 2007
    Location
    OK
    Posts
    497
    Thanks
    27
    Thanked 31 Times in 31 Posts
    Fou-Lu you're like a super hero. haha. What is the syntax error? I'll look at the rest this evening, and see if i can sort it out.
    Thanks!

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    if ($row_cnt > 0)) { has too many closing brackets.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #5
    Regular Coder stevenmw's Avatar
    Join Date
    Jun 2007
    Location
    OK
    Posts
    497
    Thanks
    27
    Thanked 31 Times in 31 Posts
    Quote Originally Posted by Fou-Lu View Post
    if ($row_cnt > 0)) { has too many closing brackets.
    Not quite sure what you mean by that? I opened two brackets so I need to close each bracket? Or maybe I'm overlooking something that I posted above?

    Here is my initial code in its entirety:
    PHP Code:
    <?php
    $mysqli 
    = new mysqli"x""x""x""x");

    if (
    mysqli_connect_errno()) {
        
    printf("Connect failed: %s\n"mysqli_connect_error());
        exit();
    }

    $query "SELECT * FROM table ORDER by id ASC";

    $ex $mysqli->query($query) or die(mysqli_error());
    $row_cnt mysqli_num_rows($ex);

    if (
    $row_cnt  0) {
    echo 
    "There are rows in the db <br />";
    }

    if (
    $result $mysqli->query($query)) {

        
    /* fetch associative array */
        
    while ($row $result->fetch_assoc()) {
            
    printf ("%s %s <br />"$row["id"], $row["title"]);
        }


        
    $result->free();
    }


    $mysqli->close($mysqli);
    ?>
    It works perfectly, but now I want to only display the while loop if $row_cnt > 0.

    So I tried this:
    PHP Code:
    <?php
    $mysqli 
    = new mysqli"x""x""x""x");

    if (
    mysqli_connect_errno()) {
        
    printf("Connect failed: %s\n"mysqli_connect_error());
        exit();
    }

    $query "SELECT * FROM table ORDER by id ASC";

    $ex $mysqli->query($query) or die(mysqli_error());
    $row_cnt mysqli_num_rows($ex);

    if (
    $row_cnt  0) {
    echo 
    "There are rows in the db <br />";
    }

    if (
    $row_cnt  0) {

        
    /* fetch associative array */
        
    while ($row $result->fetch_assoc()) {
            
    printf ("%s %s <br />"$row["bid"], $row["title"]);
        }


        
    $result->free();
    }


    $mysqli->close($mysqli);
    ?>
    But it does not work and nothing is displayed. What am I doing wrong?
    Last edited by stevenmw; 03-09-2013 at 06:04 PM.
    Thanks!

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    You have no $result variable. That should be $ex.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    stevenmw (03-09-2013)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •