Re: Help with msql_fetch_array()

From: Date: Wed, 24 Jul 2002 18:16:53 +0000
Subject: Re: Help with msql_fetch_array()
References: 1 2 3  Groups: php.general 
Request: Send a blank email to php-general+get-109336@lists.php.net to get a copy of this message
Yes, what on earth was I thinking! should be: ...
       $result = mysql_query($sql);
       if (mysql_num_rows($result)) {
        $row = mysql_fetch_assoc($result);
        } else {
        echo "whatever";
        }
... Kevin Stone wrote:
You beat me too the punch and I think you explained it better than me, but just one minor little thing to note. Where you said.. "if ( $result = mysql_query($sql)) " This is not a valid way to check if the query has returned anything. mysql_query() returns FALSE on error. So if there was no error but there also wasn't anything returned then the object stored in $result wiill more than likely evaluate to TRUE. For the determining factor you should count the number of rows with mysql_num_rows($result). If the returned value is zero then you know it hasn't returned anything. -Kevin ----- Original Message ----- From: "PHPCoder" <internet@vsa.co.za> To: "Matthew Bielecki" <Matthew.Bielecki@hobartcorp.com> Cc: "php-general" <php-general@lists.php.net> Sent: Wednesday, July 24, 2002 11:50 AM Subject: Re: [PHP] Help with msql_fetch_array()
I can almost guarantee that it's not the second line that is "failing", the problem here is that $result is not containing naything, and that is normally due to the fact that you are not connecting to the db, or the table "tablename" is not there. I use the following format as my "standard" MySQL connect and query
snippet:
$link = @mysql_connect("localhost",$username,$password) or die ('Could not connect!'); //@ suppresses the default error message generated by this function and the "or die()" bit kills the script right then and there should it not be able to connect.
       mysql_select_db("YOUR_DB_NAME",$link);
       $sql = "select * from your_table_name";
       if ( $result = mysql_query($sql)) {  // checks to see if $result
contains anything before it even tries to fetch an associative array from it.
            $row = mysql_fetch_assoc($result);
       } else {
       echo "Empty result set!";
Note also that I use mysql_fetch_assoc and NOT mysql_fecth_array, as 9 out of 10 times, you don't need the array element id's that is returned by mysql_fetch_array. Matthew Bielecki wrote:
I have a couple of scripts that fail with the error of: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL
result
resource in... I'm new to both SQL and PHP and I'm wondering if I have some setting turned off or what. Here's the piece of code that is failing (the second line fails): $result = mysql_db_query($dbname, "SELECT * FROM tablename ORDER BY id");
      $row = mysql_fetch_array($result);
Thanks for your help in advance!!
-- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php


« previous php.general (#109336) next »