Page 1 of 1
MySQL query
Posted: Sat Nov 26, 2005 3:12 pm
by InnerShadow
Code: Select all
<?php
$conn = mysql_connect("localhost", "root", "");
mysql_select_db("countries", $conn);
$result = mysql_query("SELECT SmallPlantation, MediumPlantation, LargePlantation FROM bonuses WHERE country = '$ID'")
or die(mysql_error());
while($row = mysql_fetch_assoc($result))
{
$Small = $row['SmallPlantation'];
$Medium = $row['MediumPlantation'];
$Large = $row['LargePlantation'];
}
print "$Small";
print "$Medium";
print "$Large";
?>
I was wondering if anyone could find why this script isn't working. It doesn't print anything for the variables.
Posted: Sat Nov 26, 2005 3:17 pm
by Charles256
store your query in it's own variable, i.e. mysql_query("whatever you wrote in here should e stored in a variable prior to this and then just put the variable name in here") and then echo out htat SQL statement, run it on phpmyadmin and make sure it pulls up results. just because a query runs succesfully doesn't mean it's going to get results.
Posted: Sat Nov 26, 2005 4:38 pm
by Jenk
Unless you are using register_globals (a BAD idea in this circumstance!) $ID won't contain anything, hence no rows returned.
Posted: Sat Nov 26, 2005 4:49 pm
by InnerShadow
im not sure i know exactly what you mean by storing the query in it's own variable. Could you post and example?
Posted: Sat Nov 26, 2005 4:51 pm
by InnerShadow
oh, and the $ID variable is returned on each page by a security program, so $ID has a value
Posted: Sat Nov 26, 2005 5:52 pm
by John Cartwright
debugging 101 - or die(mysql_error()) on every mysql_* call
Code: Select all
<?php
$conn = mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("countries", $conn) or die(mysql_error());
$result = mysql_query("SELECT SmallPlantation, MediumPlantation, LargePlantation FROM bonuses WHERE country = '$ID'")
or die(mysql_error());
while($row = mysql_fetch_assoc($result))
{
$Small = $row['SmallPlantation'];
$Medium = $row['MediumPlantation'];
$Large = $row['LargePlantation'];
}
print "$Small";
print "$Medium";
print "$Large";
?>
Posted: Sat Nov 26, 2005 5:54 pm
by Jenk
InnerShadow wrote:im not sure i know exactly what you mean by storing the query in it's own variable. Could you post and example?
Code: Select all
<?php
$query = "SELECT SmallPlantation, MediumPlantation, LargePlantation FROM bonuses WHERE country = '$ID'";
$result = mysql_query($query) or die(mysql_error());
?>
This allows you to echo the $query for debugging purposes (to check if the query is what you want it to be)
Posted: Sat Nov 26, 2005 5:58 pm
by Charles256
thanks for getting that for me jenks...'twas bout to post it...i lost track of the thread for a bit...
Posted: Sat Nov 26, 2005 6:31 pm
by InnerShadow
It works now, thanks a lot.