Forum Moderators: coopster
<?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
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)
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)) {
...
}
}
$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.
here is another way to figure out whats wrong. there could be 3 positions in your code where the error occurs:
$db = mysql_connect("localhost", "root", ""); 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. mysql_select_db("learndb",$db); echo([url=http://www.php.net/mysql_error]mysql_error()[/url].'<br />'); to output error information after this line again. $result = mysql_query("SELECT * FROM personnel",$db); $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. (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