sanderphp Posted May 2, 2008 Share Posted May 2, 2008 I'm getting the following error on a login script from http://www.phpeasystep.com/workshopview.php?id=6 Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/sanderan/public_html/php/cycling/checklogin.php on line 27 Wrong Username or Password Is this really a password issue or something else? Here is my code. <?php ob_start(); $host="localhost"; // Host name $username="sanderan_stats"; // Mysql username $password="password"; // Mysql password $db_name="sanderan_cycling"; // Database name $tbl_name="riders"; // Table name // Connect to server and select databse. mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); // Define $myusername and $mypassword $myusername=$_POST['myusername']; $mypassword=$_POST['mypassword']; // To protect MySQL injection (more detail about MySQL injection) $myusername = stripslashes($myusername); $mypassword = stripslashes($mypassword); $myusername = mysql_real_escape_string($myusername); $mypassword = mysql_real_escape_string($mypassword); $sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'"; $result=mysql_query($sql); // Mysql_num_row is counting table row $count=mysql_num_rows($result); // If result matched $myusername and $mypassword, table row must be 1 row if($count==1){ // Register $myusername, $mypassword and redirect to file "login_success.php" session_register("myusername"); session_register("mypassword"); header("location:login_success.php"); } else { echo "Wrong Username or Password"; } ob_end_flush(); ?> Quote Link to comment Share on other sites More sharing options...
moselkady Posted May 2, 2008 Share Posted May 2, 2008 Maybe you can print for us your query string as well as any errors from your query. ..... $sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'"; print "$sql<br>"; $result=mysql_query($sql); print mysql_error()."<br>"; ..... Quote Link to comment Share on other sites More sharing options...
sanderphp Posted May 2, 2008 Author Share Posted May 2, 2008 Unless I misunderstood what you were asking for, I added your code into mine. Here is the result: SELECT * FROM riders WHERE username='' and password='' Unknown column 'username' in 'where clause' Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/sanderan/public_html/php/cycling/checklogin.php on line 29 Wrong Username or Password Quote Link to comment Share on other sites More sharing options...
moselkady Posted May 2, 2008 Share Posted May 2, 2008 It appears that your table does not have a field called "username". As a result mysql_query($sql) fails and $result becomes invalid handle and gives the warning error when you use it afterwards. Check your table definition for the "username" field and probably it is misspelled. Quote Link to comment Share on other sites More sharing options...
sanderphp Posted May 2, 2008 Author Share Posted May 2, 2008 aha.... I was using 'login' and the example was using username. It now goes to the login_success.php page and shows me this. Anything to worry about or is this what I should be seeing? // Check if session is not registered , redirect back to main page. // Put this code in first line of web page. Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/sanderan/public_html/php/cycling/login_success.php:3) in /home/sanderan/public_html/php/cycling/login_success.php on line 4 Login Successful // Check if session is not registered , redirect back to main page. // Put this code in first line of web page. <? session_start(); if(!session_is_registered(myusername)){ header("location:main_login.php"); } ?> <html> <body> Login Successful </body> </html> Quote Link to comment Share on other sites More sharing options...
moselkady Posted May 2, 2008 Share Posted May 2, 2008 Can you post your login_success.php or first 10 lines of it? Quote Link to comment Share on other sites More sharing options...
sanderphp Posted May 2, 2008 Author Share Posted May 2, 2008 Here is all that I have in the login_success.php file // Check if session is not registered , redirect back to main page. // Put this code in first line of web page. <? session_start(); if(!session_is_registered(myusername)){ header("location:main_login.php"); } ?> <html> <body> Login Successful </body> </html> Quote Link to comment Share on other sites More sharing options...
AndyB Posted May 2, 2008 Share Posted May 2, 2008 <? // Check if session is not registered , redirect back to main page. // Put this code in first line of web page. session_start(); if(!session_is_registered(myusername)){ header("location:main_login.php"); } ?> <html> <body> Login Successful </body> </html> The above works. You needed to move the comment inside the php tags to stop it displaying - which is the output that caused the error. Quote Link to comment Share on other sites More sharing options...
sanderphp Posted May 2, 2008 Author Share Posted May 2, 2008 oh jeez!!! I can't believe that I couldn't see that. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.