Failed Query

  • DavyDuke17
  • Graduate
  • Graduate
  • DavyDuke17
  • Posts: 166

Post 3+ Months Ago

I'm not sure why this query fails, can anyone give me any suggestions, thanks.

Code: [ Select ]
//query details table begins
$sql = 'SELECT `Anaheim`.`Player` , `Anaheim`.`Avg.` , `Anaheim`.`HR` , `Anaheim`.`RBI` , `Anaheim`.`Runs` , `Anaheim`.`SB` , `Anaheim`.`OBP.` ';
$sql .= 'FROM Anaheim';
$sql .= 'ORDER BY `Anaheim`.`Player` ASC LIMIT 0, 30';

// send the query to the database
$result = mysql_query($sql) or die("Query failed");

// loop through the results
while($row = mysql_fetch_array($result))
{
  // extract the field data
  $variable1=$row["Player"];
  $variable2=$row["Avg."];
  $variable3=$row["HR"];
  $variable4=$row["RBI"];
  $variable5=$row["Runs"];
  $variable6=$row["SB"];
  $variable7=$row["OBP."];
}

?>
  1. //query details table begins
  2. $sql = 'SELECT `Anaheim`.`Player` , `Anaheim`.`Avg.` , `Anaheim`.`HR` , `Anaheim`.`RBI` , `Anaheim`.`Runs` , `Anaheim`.`SB` , `Anaheim`.`OBP.` ';
  3. $sql .= 'FROM Anaheim';
  4. $sql .= 'ORDER BY `Anaheim`.`Player` ASC LIMIT 0, 30';
  5. // send the query to the database
  6. $result = mysql_query($sql) or die("Query failed");
  7. // loop through the results
  8. while($row = mysql_fetch_array($result))
  9. {
  10.   // extract the field data
  11.   $variable1=$row["Player"];
  12.   $variable2=$row["Avg."];
  13.   $variable3=$row["HR"];
  14.   $variable4=$row["RBI"];
  15.   $variable5=$row["Runs"];
  16.   $variable6=$row["SB"];
  17.   $variable7=$row["OBP."];
  18. }
  19. ?>


I belive that is where the error would be.
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • Axe
  • Genius
  • Genius
  • User avatar
  • Posts: 5739
  • Loc: Sub-level 28

Post 3+ Months Ago

I'd take out all the ` characters, they're not needed, and you shouldn't need the "Anaheim." in there as you're telling it later on that it's from Anaheim.

just...

Code: [ Select ]
$sql = 'SELECT Player, Avg, HR, RBI, Runs, SB, OBP FROM Anaheim ORDER BY Player ASC LIMIT 0, 30';


should do it... (man, no [php] tag on here?)

And try mysql_fetch_assoc() instead of mysql_fetch_array().

Without knowing exactly what error PHP is sending back to you, I couldn't really tell ya more than that.

Try executing...

Code: [ Select ]
SELECT Player, Avg, HR, RBI, Runs, SB, OBP FROM Anaheim ORDER BY Player ASC LIMIT 0, 30


through phpMyAdmin and see if that throws back any errors.

Hmm, actually, I just noticed your one field is "Avg." and not "Avg". As "." is used to separate tables from fields, etc. you might want to try just renaming it to "Avg", and personally, I use all lower case for table & field names. Nobody sees them except the script, so it doesn't have to be gramatically correct ;)
  • RichB
  • Guru
  • Guru
  • User avatar
  • Posts: 1121
  • Loc: Boston

Post 3+ Months Ago

You're missing a space at the end of the second line again.

To help troubleshoot something like this yourself you can do the following:
Code: [ Select ]
$result = mysql_query($sql) or die("Query failed: $sql");


In other words modify the existing line, so that it shows the query string when the query fails. If you try that you'll see that when the query string is concatenated into one whole string it's missing a space.

Code: [ Select ]
'SELECT `Anaheim`.`Player` , `Anaheim`.`Avg.` , `Anaheim`.`HR` , `Anaheim`.`RBI` , `Anaheim`.`Runs` , `Anaheim`.`SB` , `Anaheim`.`OBP.` FROM AnaheimORDER BY `Anaheim`.`Player` ASC LIMIT 0, 30''


Also Axe is right that the ` character is unecessary and the query would probably be easier to read and troubleshoot without it present.
  • Axe
  • Genius
  • Genius
  • User avatar
  • Posts: 5739
  • Loc: Sub-level 28

Post 3+ Months Ago

Ahhh, good catch RichB, I never noticed the missing space at the end of the query's second line!
  • Lieven
  • Beginner
  • Beginner
  • User avatar
  • Posts: 57
  • Loc: Belgium

Post 3+ Months Ago

Just as a comment... If you see a query full of `-things, you can be 90% sure it's a query generated by PHPMyAdmin.

PHPMyAdmin is doing this to avoid making invalid queries when you have a table called "from" or "where" :wink:
  • jwh424
  • Born
  • Born
  • jwh424
  • Posts: 4

Post 3+ Months Ago

The ` character and the ' character are different.
Code: [ Select ]
//query details table begins
$sql = 'SELECT Anaheim.Player , Anaheim. Avg. , Anaheim.HR , Anaheim.RBI , Anaheim.Runs , Anaheim.SB` , Anaheim.OBP. ';
$sql .= 'FROM Anaheim';
$sql .= 'ORDER BY Anaheim.Player ASC LIMIT 0, 30';

// send the query to the database
$result = mysql_query($sql) or die("Query failed");

// loop through the results
while($row = mysql_fetch_array($result))
{
  // extract the field data
  $variable1=$row["Player"];
  $variable2=$row["Avg."];
  $variable3=$row["HR"];
  $variable4=$row["RBI"];
  $variable5=$row["Runs"];
  $variable6=$row["SB"];
  $variable7=$row["OBP."];
}

?>
  1. //query details table begins
  2. $sql = 'SELECT Anaheim.Player , Anaheim. Avg. , Anaheim.HR , Anaheim.RBI , Anaheim.Runs , Anaheim.SB` , Anaheim.OBP. ';
  3. $sql .= 'FROM Anaheim';
  4. $sql .= 'ORDER BY Anaheim.Player ASC LIMIT 0, 30';
  5. // send the query to the database
  6. $result = mysql_query($sql) or die("Query failed");
  7. // loop through the results
  8. while($row = mysql_fetch_array($result))
  9. {
  10.   // extract the field data
  11.   $variable1=$row["Player"];
  12.   $variable2=$row["Avg."];
  13.   $variable3=$row["HR"];
  14.   $variable4=$row["RBI"];
  15.   $variable5=$row["Runs"];
  16.   $variable6=$row["SB"];
  17.   $variable7=$row["OBP."];
  18. }
  19. ?>


Why are you adding "Anaheim." to the end of your table names. And, I don't believe "OBP." column name will work, but I may be wrong?

In theory, it should work after you remove the "Anaheim." and ` thingy.

Post Information

  • Total Posts in this topic: 6 posts
  • Users browsing this forum: No registered users and 68 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.