Not understanding why my tables returns empty.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Monp
Forum Newbie
Posts: 2
Joined: Mon Aug 25, 2008 9:43 am

Not understanding why my tables returns empty.

Post by Monp »

I keep on recieving my error of Error getting information yet I dont know why?

Code: Select all

 
<?php
$dbhost ='localhost';
$dbuser ='username';
$dbpass ='password';
 
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die
                    ('Error connecting to mysql');
 
$dbname ='pet';
mysql_select_db($dbname);
$result =mysql_query('SELECT * FROM pet') or die
                    ('Error getting information');
 
while ($row=mysql_fetch_array($result)) {
echo "Name:".$row{'name'}."Owner:".$row{'owner'}.
     "Specie:".$row{'species'}."Sex:".$row{'sex'}.
     "Birth:".$row{'birth'}."Death:".$row{'death'}."<br>";
}
 
mysql_close($conn);
 
?>
 
 
dajawu
Forum Commoner
Posts: 59
Joined: Fri May 23, 2008 10:16 am

Re: Not understanding why my tables returns empty.

Post by dajawu »

Well based on that error you know it is being caused on your SQL statement. Two great things I always do to figure out my SQL problems are:
1) Save SQL statements in a variable and echo them out to make sure they are correct
2) Use something like phpMyAdmin to make sure the SQL statement that you are using will work, you can just cut and copy it right into the SQL section and see what happens.

Al
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Re: Not understanding why my tables returns empty.

Post by Zoxive »

Code: Select all

$result =mysql_query('SELECT * FROM pet') or die ('Error getting information<br/>Debug INFO:<br/>'.mysql_error());
Use debug functions to narrow down your problems.
soulcrazy
Forum Newbie
Posts: 13
Joined: Sun Aug 24, 2008 10:05 pm
Location: Ontario, Canada

Re: Not understanding why my tables returns empty.

Post by soulcrazy »

Change:

Code: Select all

 
$result = mysql_query('SELECT * FROM pet') or die ('Error getting information');
 
To:

Code: Select all

 
$result = mysql_query('SELECT * FROM pet', $conn) or die ('Error getting information');
 
Also:

Code: Select all

 
$result = mysql_query("SELECT * FROM `pet`", $conn) or die ('Error getting information');   // quotes matter in some cases...
 
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Not understanding why my tables returns empty.

Post by RobertGonzalez »

The addition of the link resource identifier (in this case $conn) is not required unless you are using more than one connection to the same server. In MySQL yhe backticks are always a good idea around database, table and column names in the event MySQL decides to add reserved words in the future.

Right now though I would imagine that your best help would come from a simple call to mysql_error() in the event the query fails.

Code: Select all

<?php
$sql = 'SELECT * FROM `pets`';
 
if (!$result = mysql_query($sql)) {
  die('Query had a problem: ' . mysql_error()); // You do not want mysql_error() in production!
}
 
// ... use the result here as you wish
?>
Monp
Forum Newbie
Posts: 2
Joined: Mon Aug 25, 2008 9:43 am

Re: Not understanding why my tables returns empty.

Post by Monp »

It says no database selected but I thought I was selecting one with the call of mysql_select_db.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Not understanding why my tables returns empty.

Post by RobertGonzalez »

Same rules apply here when it comes to trapping errors. Try this:

Code: Select all

<?php
if (!mysql_select_db($dbname)) {
  die("Cannot select the database $dbname: " . mysql_error()); // Again do not use this in production
}
?>
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Not understanding why my tables returns empty.

Post by califdon »

Is your database name "pet" AND your table name "pet"? Are you sure?
Post Reply