View Full Version : Error while trying to display contents of database with PHP

07-24-2011, 02:07 AM
I've been searching for answers to this problem for a little while now to no avail. Im hesitant to post a uqestion that may have been answered, but here it goes.

I'm learning how to use MySQL and PHP. I have created the database, but when I try to display it I get the following error.

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/bail/public_html/bailfiles/index.php on line 15

I have edited the syntax over and over and over and over but I either get a new error until I reverse the tweak, or I get the same message.

Here is the database information

DatabaseName: bail_bail
TableName: bailfiles
contains the following data fields:
id = 4 digit integer, auto-increment
fname = 30 digits varchar
lname = 30 digit varchar
phon = 12 digit varchar

Here is the code in my index.php file

Smith Bail Files Database by Justin
<body bgcolor="000000" text="00ff00" link="00ff00" vlink="00ff00">


mysql_connect ("localhost", "DATABASEUSERNAME", "DATABASEPASSWORD") or die ('Could not connect to the MySQL Database because : ' . mysql_error());
mysql_select_db ("bail_bail ");
$query = mysql_query ("SELECT * FROM bailfiles ");
while ($row = mysql_fetch_array(query))

echo "<br />ID: ".$row[id];
echo "<br />First Name: ".$row[fname];
echo "<br />Last Name: ".$row[lname];
echo "<br />Phone: ".$row[phon];



I have verified numerous times I am using the proper variable names and database information. I am completely stuck. I wouldn't be asking for help if I thought I could hack it until I figured it out, but Im getting burnt on the topic.

Any ideas?

07-24-2011, 02:05 PM
Just a little typo, you have written query instead of $query inside the mysql_fetch_array() function :) Everything else looks alright, although you might want to encase your array keys in the echo statements in quotation marks.

07-24-2011, 09:10 PM
Unfortunately that did not solve my problem. Same error message. I'm wondering if I should use another method of displaying the data.

07-24-2011, 09:30 PM
After looking at your code again, I noticed you had nothing to catch mysql errors in the query. Use this instead:

mysql_select_db ("bail_bail") or die(mysql_error());
$query = mysql_query ("SELECT * FROM bailfiles") or die(mysql_error());
while ($row = mysql_fetch_array($query))

I suspect your DB isn't connecting or there's no table called bailfiles, or no database called bail_bail. I also noticed that there was a space after bail_bail and the quotation mark which could have been causing the problem and again, you weren't catching errors there.

It's important to account for every single error to ensure that you not only know what's going on on your code, but you prevent potential bugs that create security risks and various other unexpected results.

07-25-2011, 04:37 AM
your corrected code worked like a charm. You are the man, thank you very much.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum