MySQL query

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
InnerShadow
Forum Commoner
Posts: 37
Joined: Thu Nov 10, 2005 10:44 pm
Location: US

MySQL query

Post 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.
Charles256
DevNet Resident
Posts: 1375
Joined: Fri Sep 16, 2005 9:06 pm

Post 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.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

Unless you are using register_globals (a BAD idea in this circumstance!) $ID won't contain anything, hence no rows returned.
InnerShadow
Forum Commoner
Posts: 37
Joined: Thu Nov 10, 2005 10:44 pm
Location: US

Post 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?
Last edited by InnerShadow on Sat Nov 26, 2005 5:24 pm, edited 2 times in total.
InnerShadow
Forum Commoner
Posts: 37
Joined: Thu Nov 10, 2005 10:44 pm
Location: US

Post by InnerShadow »

oh, and the $ID variable is returned on each page by a security program, so $ID has a value
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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";

?>
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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)
Charles256
DevNet Resident
Posts: 1375
Joined: Fri Sep 16, 2005 9:06 pm

Post by Charles256 »

thanks for getting that for me jenks...'twas bout to post it...i lost track of the thread for a bit...
InnerShadow
Forum Commoner
Posts: 37
Joined: Thu Nov 10, 2005 10:44 pm
Location: US

Post by InnerShadow »

It works now, thanks a lot.
Post Reply