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 4 of 4
  1. #1
    New Coder
    Join Date
    Nov 2007
    Posts
    50
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Question Should be simple... echo count of rows in MySQL table

    PHP Code:
    <?php

    $server
    ="localhost";
    $user="user";
    $password="password";
    $database="database";

    $db = new mysqli($server,$user,$password,$database);
    if (
    $db->connect_errno) {
    printf("Connect failed: %s\n"$mysqli->connect_error);
    exit();
    }

    $q 'SELECT COUNT(*) FROM example';
    $r=mysqli_query($db,$q) or die(mysqli_error($db)." Q=".$q);
    $r mysql_fetch_row($r);
    echo 
    $r[0];

    ?>
    For the life of me, I cannot see why this returns 0, or print_r($db) -> empty SQL array
    Code:
    mysqli_result Object ( )

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,980
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Your issue is that you are combining MySQLi and MySQL libraries together. If you enable your error reporting it should complain that mysql_fetch_array expects a resource, but the mysqli_query returns a mysqli_result object.
    Simply add the 'i' into the mysqli_fetch_row and it will work fine.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #3
    New Coder
    Join Date
    Nov 2007
    Posts
    50
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Never in my life have I spent so much time on such a tiny thing. I hope somebody finds this page someday, thanks.

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,980
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Quote Originally Posted by epheterson View Post
    Never in my life have I spent so much time on such a tiny thing. I hope somebody finds this page someday, thanks.
    Lol, its amazing what one letter difference can do
    Since you are already using the $db in an object oriented fashion with the error checking, why not continue doing so in the rest of the execution? That would get around some of the problems like above. The incorrect parameter type should be classified by an E_WARNING level, while if you have the incorrect method completely it would be E_ERROR. So at least you'd get a white output if you don't have error reporting enabled versus normal output missing the desired data.
    OO mysqli also removes the mysqli_ from the prefix of the methods, so its more difficult to make an error on that one.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 


  •  

    Posting Permissions

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