Help getting values from MySQL database

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
tomjacko
Forum Newbie
Posts: 1
Joined: Fri Jan 14, 2011 2:20 pm

Help getting values from MySQL database

Post 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");
?>
danwguy
Forum Contributor
Posts: 256
Joined: Wed Nov 17, 2010 1:09 pm
Location: San Diego, CA

Re: Help getting values from MySQL database

Post 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.
Post Reply