Resource id #3 error using mysql_num_row

Discussion in 'PHP' started by ducca, Nov 23, 2006.

  1. #1
    I'm trying to write a simple login script but I'm getting the resource id #3 error,
    Hope someone can help
    thanks in advance
    
    <?php
    $host="localhost"; 
    $user="root";
    $password="";
    $db="admin";
    $tableadmin="admin1";
    
    $adminuser= $_POST["adminuser"];
    $adminpass= $_POST["adminpass"];
    
    $link = mysql_connect($host,$user,$password)
    or die ("couldn't connect to server");
    $db = mysql_select_db($db,$link)
    or die ("Couldn't select database");
    
     $result = "SELECT * FROM admin1 WHERE adminuser='$adminuser'  AND adminpass='$adminpass'";			
    	$query = mysql_query($result)
    	or die ("Couldn't execute query.");  
    	echo "<br><br>$query<br><br>";
    	//heres the problem 
    if (mysql_num_rows($result) > 0)	
    {
    $_SESSION["authenticatedUser"]= $adminuser;
    header("Location: loggedon.php");
    }
    else
    {
    $_SESSION["message"]="could not connect to database as 	$adminuser";
    header("Location: loginform.php"); 
    }
    ?>  
    		
    
    
    PHP:

     
    ducca, Nov 23, 2006 IP
  2. lbalance

    lbalance Peon

    Messages:
    381
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #2
    why wouldnt the SQL statement be called $query and
    the mysql_query be stored as the result?

    the the if mysql_num_rows will be pulling the real result
    and not the SQL query
     
    lbalance, Nov 23, 2006 IP
  3. ducca

    ducca Peon

    Messages:
    20
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks for that,
    Changed it to this,
    
     $query = "SELECT * FROM admin1 WHERE adminuser='$adminuser'  AND adminpass='$adminpass'";			
    	$result = mysql_query($query)
    	or die ("Couldn't execute query.");  
    	
    	//heres the problem 
    if (mysql_num_rows($result) > 0)	
    {
    $_SESSION["authenticatedUser"]= $adminuser;
    header("Location: loggedon.php");
    }
    
    
    PHP:

    still same error
    Thanks for any help
     
    ducca, Nov 23, 2006 IP
  4. lbalance

    lbalance Peon

    Messages:
    381
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #4
    try using the COUNT(*) instead

    
    $query = "SELECT COUNT(*) as cnt FROM admin1 WHERE adminuser='$adminuser'  AND adminpass='$adminpass'";			
    
    $result = mysql_query($query) or die ("Couldn't execute query.");  
    
    $row = mysql_fetch_array($result);
    
    if ($row['cnt'] > 0)	
    {
    $_SESSION["authenticatedUser"]= $adminuser;
    header("Location: loggedon.php");
    }
    
    
    PHP:
     
    lbalance, Nov 23, 2006 IP
  5. ducca

    ducca Peon

    Messages:
    20
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Still not working but thanks for your help
    must be something simple but just can,t see it
    cheers
     
    ducca, Nov 23, 2006 IP
  6. lbalance

    lbalance Peon

    Messages:
    381
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #6
    try putting a @ before mysql_query

    
    $result = @mysql_query($query) or die ("Couldn't execute query.");
    
    PHP:
     
    lbalance, Nov 23, 2006 IP
  7. ducca

    ducca Peon

    Messages:
    20
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    still not working
    thanks for any help
     
    ducca, Nov 24, 2006 IP
  8. decepti0n

    decepti0n Peon

    Messages:
    519
    Likes Received:
    16
    Best Answers:
    0
    Trophy Points:
    0
    #8
    What comes up as the error? :s
     
    decepti0n, Nov 24, 2006 IP
  9. ducca

    ducca Peon

    Messages:
    20
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    This is the error

    I echo the error here

    
    echo "<br>$query<br>";
    
    PHP:
    and this is the result when I try to login from the login form

    Resource id #3


    Warning: Cannot modify header information - headers already sent by (output started at C:\Program Files\xampp\htdocs\admin_log_in.php:18) in C:\Program Files\xampp\htdocs\admin_log_in.php on line 23



    Thanks for any help
    Regards
    Ducca
     
    ducca, Nov 24, 2006 IP
  10. lbalance

    lbalance Peon

    Messages:
    381
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #10
    this error is from echo'ing data before the header function.
     
    lbalance, Nov 24, 2006 IP
  11. maiahost

    maiahost Guest

    Messages:
    664
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Check your HTML for blank lines
     
    maiahost, Nov 24, 2006 IP
  12. lbalance

    lbalance Peon

    Messages:
    381
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #12


    if you switched around the $query and $result variables as mentioned earlier in this thread, then $query should echo your SQL statement. if it is showing the Resource ID, then $query is storing the mysql_query still which is usually stored as $result. any time you echo mysql_query, you get a resource id.
     
    lbalance, Nov 24, 2006 IP
  13. decepti0n

    decepti0n Peon

    Messages:
    519
    Likes Received:
    16
    Best Answers:
    0
    Trophy Points:
    0
    #13
    Yeah, its all fine, just echo $result instead, and then the header() function shouldn't output errors since nothing else has come before it
     
    decepti0n, Nov 24, 2006 IP
  14. Chemo

    Chemo Peon

    Messages:
    146
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #14
    
    	/*
    	 * Setup connection params
    	 * Bobby Easland, 11 October 2006
    	 */
    	$host				=	"localhost";
    	$user				=	"root";
    	$password		=	"";
    	$db					=	"admin";
    	$tableadmin	=	"admin1";
    	
    	/*
    	 * Hopefully sanitize the values coming in
    	 * Bobby Easland, 11 October 2006
    	 */
    	$adminuser	= $_POST["adminuser"];
    	$adminpass	= $_POST["adminpass"];
    	
    	/*
    	 * Connect to DB or die with error
    	 * Bobby Easland, 11 October 2006
    	 */
    	$link	= mysql_connect($host,$user,$password) or die ("couldn't connect to server");
    	$db		= mysql_select_db($db,$link) or die ("Couldn't select database");
    	
    	/*
    	 * Initialize the SQL statement
    	 * Bobby Easland, 11 October 2006
    	 */
    	$query = "SELECT * 
    						FROM admin1 
    						WHERE adminuser='" . mysql_real_escape_string($adminuser) . "' 
    							AND adminpass='" . mysql_real_escape_string($adminpass) . "' 
    						LIMIT 1";   
    	$result = mysql_query($result) or die ( 'MySQL error: ' . mysql_error() . '<br>' . $query ); 
    
    	/*
    	 * Basic sanity check
    	 * Bobby Easland, 11 October 2006
    	 */
    	if ( mysql_num_rows($result) > 0 ){
    		$_SESSION["authenticatedUser"] = $adminuser;
    		header("Location: loggedon.php");
    	} else {
    	 /*
    	  * No rows returned...unset session user var and header away
    	  * Bobby Easland, 11 October 2006
    	  */
    		unset($_SESSION["authenticatedUser"]);
    		$_SESSION["message"] = 'could not authenticate: ' . $adminuser;
    		header("Location: loginform.php");
    	}
    	
    	/*
    	 * No headers sent...might as well exit or die with grace
    	 * Bobby Easland, 11 October 2006
    	 */
    	exit();
    
    PHP:
     
    Chemo, Nov 24, 2006 IP
  15. ducca

    ducca Peon

    Messages:
    20
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #15
    Cheers all,
    thanks very much for your help
    cheers for the help Chemo,
    I,ve used your code now I,m getting this error


    MySQL error: Query was empty
    SELECT * FROM admin WHERE adminuser='1' AND adminpass='1' LIMIT 1

    the database is fine and the values are in the correct table

    thanks again
     
    ducca, Nov 26, 2006 IP
  16. daboss

    daboss Guest

    Messages:
    2,249
    Likes Received:
    151
    Best Answers:
    0
    Trophy Points:
    0
    #16
    run the query:
    SELECT * FROM admin WHERE adminuser='1' AND adminpass='1' LIMIT 1
    Code (markup):
    directly into phpmyadmin and see if it actually returns a result. i suspect your database does not contain such a record.
     
    daboss, Nov 26, 2006 IP
  17. ducca

    ducca Peon

    Messages:
    20
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #17
    Cheers all, appreciate the help
    the database in phpmyadmin was showing the values correctly,
    dropped the tables and created them again and now it works fine
    thanks again everyone
    regards
    Ducca
     
    ducca, Nov 26, 2006 IP
  18. Icheb

    Icheb Peon

    Messages:
    1,092
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #18
    A query without a result doesn't throw an error. :D
     
    Icheb, Nov 26, 2006 IP
  19. Luke

    Luke Peon

    Messages:
    111
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #19
    Basic 101 but remember to mysql_real_escape_string() your incoming POST vars when coding your own scripts.
     
    Luke, Nov 27, 2006 IP
  20. Barti1987

    Barti1987 Well-Known Member

    Messages:
    2,703
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    185
    #20
    
    <?php
    //Connect To Database
    $host="localhost"; 
    $user="root";
    $password="";
    $db="admin";
    $tableadmin="admin1";
    $link = mysql_connect($host,$user,$password) or die (mysql_error());
    $db = mysql_select_db($db,$link) or die (mysql_error());
    
    //Get userinfo
    $adminuser= strip_tags($_POST["adminuser"]);
    $adminpass= strip_tags($_POST["adminpass"]);
    
    //Run Query
    $query = mysql_query("SELECT COUNT(*) FROM `admin1` 
    	WHERE `adminuser`='$adminuser' AND `adminpass`='$adminpass'") or $error = mysql_error(); 
    
    if($errro == NULL){
      $count = mysql_fetch_row($query) or $error = mysql_error();
    }
    
    if($count[0] > 0){
    	$_SESSION["authenticatedUser"] = $adminuser;
    	header("Location: loggedon.php");
    	exit();
    }
    
    if($error){ 
    	$errro_message = 'Internal Error: <br/>'.$error;
    } else {
    	$error_message = 'Invalid Username and/or Password';
    }
    
    
    //This page is presumed to be loginform.php, else this code should be placed topmost of loginform.php
    
    echo $error_message;
    ?>
    
    PHP:
    Peace,
     
    Barti1987, Nov 27, 2006 IP