Page 1 of 1

Not understanding why my tables returns empty.

Posted: Mon Aug 25, 2008 9:49 am
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);
 
?>
 
 

Re: Not understanding why my tables returns empty.

Posted: Mon Aug 25, 2008 10:18 am
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

Re: Not understanding why my tables returns empty.

Posted: Mon Aug 25, 2008 1:16 pm
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.

Re: Not understanding why my tables returns empty.

Posted: Mon Aug 25, 2008 1:31 pm
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...
 

Re: Not understanding why my tables returns empty.

Posted: Mon Aug 25, 2008 2:57 pm
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
?>

Re: Not understanding why my tables returns empty.

Posted: Mon Aug 25, 2008 10:39 pm
by Monp
It says no database selected but I thought I was selecting one with the call of mysql_select_db.

Re: Not understanding why my tables returns empty.

Posted: Mon Aug 25, 2008 11:39 pm
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
}
?>

Re: Not understanding why my tables returns empty.

Posted: Wed Aug 27, 2008 1:07 am
by califdon
Is your database name "pet" AND your table name "pet"? Are you sure?