Go Back   CodingForums.com > :: Server side development > MySQL

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-24-2011, 01:07 AM   PM User | #1
H3mp
New Coder

 
Join Date: Jul 2011
Location: Pennsylvania
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
H3mp is an unknown quantity at this point
Error while trying to display contents of database with PHP

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.

Code:
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
Code:
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
Code:
<html>
<head>
<title>
Smith Bail Files Database by Justin
</title>
</head>
<body bgcolor="000000" text="00ff00" link="00ff00" vlink="00ff00">


<?php

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];
}

?>


</body>
</html>
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?
H3mp is offline   Reply With Quote
Old 07-24-2011, 01:05 PM   PM User | #2
BluePanther
Senior Coder

 
Join Date: Jul 2011
Posts: 1,226
Thanks: 3
Thanked 171 Times in 171 Posts
BluePanther is on a distinguished road
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.
BluePanther is offline   Reply With Quote
Old 07-24-2011, 08:10 PM   PM User | #3
H3mp
New Coder

 
Join Date: Jul 2011
Location: Pennsylvania
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
H3mp is an unknown quantity at this point
Unfortunately that did not solve my problem. Same error message. I'm wondering if I should use another method of displaying the data.
H3mp is offline   Reply With Quote
Old 07-24-2011, 08:30 PM   PM User | #4
BluePanther
Senior Coder

 
Join Date: Jul 2011
Posts: 1,226
Thanks: 3
Thanked 171 Times in 171 Posts
BluePanther is on a distinguished road
After looking at your code again, I noticed you had nothing to catch mysql errors in the query. Use this instead:
PHP Code:
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.
BluePanther is offline   Reply With Quote
Old 07-25-2011, 03:37 AM   PM User | #5
H3mp
New Coder

 
Join Date: Jul 2011
Location: Pennsylvania
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
H3mp is an unknown quantity at this point
your corrected code worked like a charm. You are the man, thank you very much.
H3mp is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:13 PM.


Advertisement
Log in to turn off these ads.