Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Oct 2011
    Posts
    92
    Thanks
    38
    Thanked 0 Times in 0 Posts

    mysql_result error

    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
    Code:
    // 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.')) 
    			window.location.href='register.php'; 
    		else
    			window.location.href='index.php';
    		</script>");
    		}
    	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;
    		}

  • #2
    New Coder
    Join Date
    Feb 2008
    Posts
    39
    Thanks
    0
    Thanked 1 Time in 1 Post
    This probably isn't it...but doesn't your select statement need to look like

    Code:
    PHP Code:
    $query "SELECT id FROM $tb_name WHERE username='$myusername' AND password='$password_hash'"

  • Users who have thanked golffor1 for this post:

    hans_cellc (10-18-2012)

  • #3
    New Coder
    Join Date
    Oct 2011
    Posts
    92
    Thanks
    38
    Thanked 0 Times in 0 Posts
    That worked perfectly thanks, all I had to do was to remove the single quotations around id in the SELECT.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •