Page 1 of 1

Help getting values from MySQL database

Posted: Fri Jan 14, 2011 2:31 pm
by tomjacko
Hello, I need help, as i'm trying to get a value from my user database which is the user ID (Auto Increment), when the user name is a specific value. For example when the user name is "UserTwo" the database gets the ID associated from that row which would be Two as it's the second user. However due to my lack of PHP knowlage I cannot understand why the $ID variable is blank. :( Any help would really be appreciated.

Code: Select all

<?PHP
 // Connects to your Database
 include("db.php");
 mysql_connect("$host", "$user", "$pass") or die(mysql_error()); 
 mysql_select_db("$dbname") or die(mysql_error());

$query  = ("SELECT ID FROM  `users` WHERE username ='userone' ORDER BY  `ID` ASC LIMIT 0 , 30");

$result = mysql_query($query);
$ID = "$result['ID']";
echo("$ID");
?>

Re: Help getting values from MySQL database

Posted: Fri Jan 14, 2011 2:48 pm
by danwguy
I would use something more along the lines of this...

Code: Select all

<?PHP
 // Connects to your Database
 include("db.php");
 mysql_connect("$host", "$user", "$pass") or die(mysql_error()); 
 mysql_select_db("$dbname") or die(mysql_error());

$query  = ("SELECT ID FROM  `users` WHERE username ='userone' ORDER BY  `ID` ASC LIMIT 0 , 30");

$result = mysql_query($query);
while($row = mysql_fetch_array($result)) {
$ID = $row['ID'];
echo $ID;
?>
That will pull all the information from that entire row and save $ID as the ID field, then on your echo I woulnd't use echo ('$ID');
just use echo $ID and you should be good to go.