View Full Version : mysql_result error

10-18-2012, 07:53 AM
I have the following code and it is suppose to give me the user id which is the primary auto increment value specific to each user. However no matter which user I use it return id instead of the value.
Please help

I am already connected to the DB at this point

// Table name variable
$tb_name = "tb_user";

// Set the Query within a variable
// I have sed SELECT 'id' instead of SELECT * as it speeds up the search especially if you have thousands of users
$query = "SELECT 'id' FROM $tb_name WHERE username='$myusername' AND password='$password_hash'";
if($result = mysql_query($query)) {
// Mysql_num_row is counting table row result
$count = mysql_num_rows($result);

// Create a if statement to check if results returned and if not to display an error
// I have created these messages with window.alert and window.confirm just to be snazzy
// I am able to refer back to the index.php on both conditions as there is a conditional to change the menu links if user logged in.
if($count == 0) {
// incorrect ask user if they wish to register or try again
echo ("<script type='text/javascript'>
if (window.confirm('Username or Password INCORRECT! Press OK to Register a new user or CANCEL to try again.'))
else {
// Register SESSION and redirect to file "securepage.php"
// Set myusername session variable
echo $user_id = mysql_result($result, 0, 'id');
$_SESSION['user_id'] = $user_id;

10-18-2012, 08:29 AM
This probably isn't it...but doesn't your select statement need to look like

$query = "SELECT id FROM $tb_name WHERE username='$myusername' AND password='$password_hash'";

10-18-2012, 08:51 AM
That worked perfectly thanks, all I had to do was to remove the single quotations around id in the SELECT.