Welcome to WebmasterWorld Guest from 35.173.48.224

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

PHP/MySQL error

Keep getting PHP/MySQL erro

     

avsfan

4:30 am on Mar 2, 2003 (gmt 0)

Inactive Member
Account Expired

 
 


I'm new to PHP and MySQL and I'm using a tutorial on a website to help me out. I've created the database and the information is there. Here's the simple script:

<?php
$db = mysql_connect("localhost", "root", "");
mysql_select_db("learndb",$db);
$result = mysql_query("SELECT * FROM personnel",$db);
echo "<TABLE>";
echo"<TR><TD><B>Full Name</B><TD><B>Nick Name</B><TD><B>Salary</B></TR>";
while ($myrow = mysql_fetch_array($result))
{
echo "<TR><TD>";
echo $myrow["firstname"];
echo " ";
echo $myrow["lastname"];
echo "<TD>";
echo $myrow["nick"];
echo "<TD>";
echo $myrow["salary"];
}
echo "</TABLE>";
?>

When I call the page up, I keep getting this error message:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/cdvomaha/public_html/firstphp/viewdb.php on line 17

The line it is referring to is:

while ($myrow = mysql_fetch_array($result))

Any suggestions on how to get this to work?

Thanks in advance,

Barry

5:31 am on Mar 2, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Apr 22, 2002
posts:2546
votes: 0


Welcome to Webmaster World!

This is how I connect to MySQL [mysql.com]


$db = mysql_connect("localhost", "username_dbusername", "password");
mysql_select_db [php.net]("username_learndb");
$query = "SELECT [mysql.com] * FROM personnel";
$result = mysql_query [mysql.com]($query);

Where username is your login name to your account and dbusername is your database username.(I don't know about "root" because I just pay to host my sites)

8:49 am on Mar 2, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Oct 15, 2002
posts:710
votes: 0


If I remember correctly, that could be because the database is empty (or your query did not match any records). Try this...

After this line:

$result = mysql_query("SELECT * FROM personnel",$db);

Add this:

$NumRows = mysql_num_rows($result);

Then wrap your WHILE statements in this IF/ELSE expression:

if ($NumRows == 0) {

echo "Query produced no results!";

} else {

while (myrow = mysql_fetch_array($result)) {
...
}
}

DanCld

10:06 am on Mar 2, 2003 (gmt 0)

Inactive Member
Account Expired

 
 


Your problem is likely to have been caused by an error when executing the "SELECT * FROM personnel" query. You may try the following hack in order to see what's going on:

$result = mysql_query("SELECT * FROM personnel",$db) or die("select: ".mysql_error());

As for getting each row, there is no need to check the number of lines returned (at least for this script). I would use the following line instead, in order to check also for the type of the value returned by mysql_fetch_array():

while (($myrow = mysql_fetch_array($result))!== false)

Hope this helps.

11:31 am on Mar 2, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Jan 7, 2003
posts:1230
votes: 0


hi avsfan,

here is another way to figure out whats wrong. there could be 3 positions in your code where the error occurs:

  1. $db = mysql_connect("localhost", "root", "");

    check $db after this line. just insert
    echo("<tt>$db</tt><br />");
    to check wether it's false or a valid result identifier. if it's false, then the connection to the mysql server failed. use
    echo([url=http://www.php.net/mysql_error]mysql_error()[/url].'<br />');
    to output error information then. it can be the server-address (localhost), or the username and password combo.
  2. mysql_select_db("learndb",$db);

    here again: check wether the database learndb exists or not. use
    echo([url=http://www.php.net/mysql_error]mysql_error()[/url].'<br />');
    to output error information after this line again.
  3. $result = mysql_query("SELECT * FROM personnel",$db); 

    this is quite like the connect. your query can be wrong and that's all. if
    $db
    is a valid resource identifier, then use check
    $result
    afterwards with a simple output:
    echo("<tt>$result</tt><br />");
    . if this is false, use
    echo([url=http://www.php.net/mysql_error]mysql_error()[/url].'<br />');
    to output error information again.

hope this will give you more information on your error(s) and will lead to a solution. if not, post detailed errorinfo and the section where the error appears.
5:19 pm on Mar 4, 2003 (gmt 0)

Junior Member

10+ Year Member

joined:Jan 14, 2002
posts:118
votes: 0


I find it helpful to have the command stored in a seperate variable, so a query in my scripts would look like this:

(assume that I've connected aand stored the link id in $link_id)

$command = "SELECT * FROM blurgh";
$result = mysql_query ($command, $link_id) or die ("<p>COMMAND: $command</p><p>ERROR: ". mysql_error ());

Allen