pes96 Posted July 23, 2012 Share Posted July 23, 2012 PHP Error Message Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource /home/a4199910/public_html/login2.php on line 28 http://teste-vieirasousa.tk/login2.php THE CODE: (the values of database, password, username, server that i use here are an example) <?php $link = mysql_connect("localhost", "username", "password"); mysql_select_db("database_name", $link); function login($username, $password) { $username = addslashes($username); $password = md5($password); $query = mysql_query("SELECT * FROM user_accounts WHERE username='$username' AND password='$password'"); if(mysql_num_rows($query) == 1) { $info = mysql_fetch_array($query); $userid = $info[userid]; $sessionid = md5($userid . time()); $time = time(); @setcookie ('test_account', $sessionid, $time+3600, '/', ''); mysql_query("DELETE FROM user_sessions WHERE userid='$userid'"); mysql_query("INSERT INTO user_sessions (sessionid,userid,timestamp) VALUES('$sessionid','$userid','$time')"); return $userid; } else { return 0; } } function status() { $sessionid = $_COOKIE[test_account]; $oldtime = time() - 3600; $query = mysql_query("SELECT * FROM user_sessions WHERE sessionid='$sessionid' AND timestamp>$oldtime"); if(mysql_num_rows($query) == 1) { $info = mysql_fetch_array($query); return $info[userid]; } return 0; } function logout() { $sessionid = $_COOKIE[test_account]; @setcookie ("test_account",'', time()-99999, '/', ''); mysql_query("DELETE FROM user_sessions WHERE sessionid='$sessionid'"); } if($_POST[username] !='' || $_POST[password] != '') { $login_status = login($_POST[username], $_POST[password]); } else if($_GET[logout]) { logout(); } $userid = status(); if($userid > 0) { echo "Welcome to our site, user #$userid (<a href='?logout'>Click here to logout</a>)"; } else { if($login_status != '' && $login_status == 0) { echo "Invalid username/password combo.<br>"; } ?> <form action="sample.php" method="POST"> <input type=text name=username> <input type=password name=password> <input type=submit value="Log In"> </form> <?php } ?> LINE 28: if(mysql_num_rows($query) == 1) { Anyone can help me to resolve this error? Thank you Quote Link to comment Share on other sites More sharing options...
xflawless Posted July 23, 2012 Share Posted July 23, 2012 Add LIMIT 1 to your query. SELECT * FROM user_accounts WHERE username='$username' AND password='$password' LIMIT 1 Quote Link to comment Share on other sites More sharing options...
jazzman1 Posted July 23, 2012 Share Posted July 23, 2012 Your query is wrong I think. Copy/paste this: $query = mysql_query("SELECT * FROM user_accounts WHERE username='".$username."' AND password='".$password."'"); Quote Link to comment Share on other sites More sharing options...
pes96 Posted July 23, 2012 Author Share Posted July 23, 2012 And i have to put this on line 28? Thanks Quote Link to comment Share on other sites More sharing options...
jazzman1 Posted July 23, 2012 Share Posted July 23, 2012 You have to learn escaping single quotes in "php varibles". Take a look at the example: $str1 = 'string'; $str2 = '$str1'; printf('%s and %s', $str1, $str2); Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted July 23, 2012 Share Posted July 23, 2012 The query that is failing, based on the line number being listed is the following - $query = mysql_query("SELECT * FROM user_sessions WHERE sessionid='$sessionid' AND timestamp>$oldtime"); You need to have error checking and error reporting logic in your code to get your code to tell you when and why something is failing. Try the following for debugging purposes - $query = mysql_query("SELECT * FROM user_sessions WHERE sessionid='$sessionid' AND timestamp>$oldtime") or die(mysql_error()); Quote Link to comment Share on other sites More sharing options...
pes96 Posted July 23, 2012 Author Share Posted July 23, 2012 I do this, but appears the same error. Quote Link to comment Share on other sites More sharing options...
pes96 Posted July 23, 2012 Author Share Posted July 23, 2012 You have to learn escaping single quotes in "php varibles". Take a look at the example: $str1 = 'string'; $str2 = '$str1'; printf('%s and %s', $str1, $str2); So you can explain me where is the error in my code? Thanks for all Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted July 23, 2012 Share Posted July 23, 2012 I do this, but appears the same error. What is your current code? And is there any chance you are using a cheap/free web host where changes you make to your files don't take affect immediately due to disk caching? Quote Link to comment Share on other sites More sharing options...
pes96 Posted July 23, 2012 Author Share Posted July 23, 2012 I do this, but appears the same error. What is your current code? And is there any chance you are using a cheap/free web host where changes you make to your files don't take affect immediately due to disk caching? Now i have done and appears the message: "No database selected" Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted July 23, 2012 Share Posted July 23, 2012 Your mysql_select_db() statement is failing, either because the database name you are putting into it doesn't exist or if it does, your database user has not been assigned permission to access it. Again, all code needs error checking and error reporting logic to get it to tell you when and why it is failing. Use the following in place of your mysql_select_db statement - mysql_select_db("database_name", $link) or die("Select db failed: " . mysql_error()); Quote Link to comment Share on other sites More sharing options...
pes96 Posted July 23, 2012 Author Share Posted July 23, 2012 Your mysql_select_db() statement is failing, either because the database name you are putting into it doesn't exist or if it does, your database user has not been assigned permission to access it. Again, all code needs error checking and error reporting logic to get it to tell you when and why it is failing. Use the following in place of your mysql_select_db statement - mysql_select_db("database_name", $link) or die("Select db failed: " . mysql_error()); Now appears: "Select db failed: Access denied for user 'a4199910_login'@'10.1.1.43' to database 'Login'" Quote Link to comment Share on other sites More sharing options...
jazzman1 Posted July 23, 2012 Share Posted July 23, 2012 So you can explain me where is the error in my code? Thanks for all My apologies, your sql syntax is correct :-\ First, I thought that the line 28 is just before the query I gave you above. Secondly, you can make a simple debugging test to check, what actually values you will send to database. <?php $username = 'jazzman'; $password = 'password'; $id = 1; $sql1 = "SELECT * FROM user_accounts WHERE username='".$username."' AND password='".$password."' AND user_id='".$id."'"; $sql2 = 'SELECT * FROM user_accounts WHERE username ='.$username.' AND password='.$password.' AND user_id='.$id; // integer id $sql3 = "SELECT * FROM user_accounts WHERE username='$username' AND password='$password' AND user_id='$id'"; $sql4 = 'SELECT * FROM user_accounts WHERE username=$username AND password=$password'; // invalid query printf('%s<br />%s<br />%s <br />%s', $sql1, $sql2, $sql3, $sql4); Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted July 23, 2012 Share Posted July 23, 2012 Now appears: "Select db failed: Access denied for user 'a4199910_login'@'10.1.1.43' to database 'Login'" After you created the database user and the database, did you assign that user permissions to access that database? Quote Link to comment Share on other sites More sharing options...
pes96 Posted July 23, 2012 Author Share Posted July 23, 2012 So you can explain me where is the error in my code? Thanks for all My apologies, your sql syntax is correct :-\ First, I thought that the line 28 is just before the query I gave you above. Secondly, you can make a simple debugging test to check, what actually values you will send to database. <?php $username = 'jazzman'; $password = 'password'; $id = 1; $sql1 = "SELECT * FROM user_accounts WHERE username='".$username."' AND password='".$password."' AND user_id='".$id."'"; $sql2 = 'SELECT * FROM user_accounts WHERE username ='.$username.' AND password='.$password.' AND user_id='.$id; // integer id $sql3 = "SELECT * FROM user_accounts WHERE username='$username' AND password='$password' AND user_id='$id'"; $sql4 = 'SELECT * FROM user_accounts WHERE username=$username AND password=$password'; // invalid query printf('%s<br />%s<br />%s <br />%s', $sql1, $sql2, $sql3, $sql4); So i have to put this code where? Quote Link to comment Share on other sites More sharing options...
jazzman1 Posted July 23, 2012 Share Posted July 23, 2012 So i have to put this code where? I think, you need to start learning php from beginning - step by step . Quote Link to comment Share on other sites More sharing options...
pes96 Posted July 23, 2012 Author Share Posted July 23, 2012 So i have to put this code where? I think, you need to start learning php from beginning - step by step . But you can only help me correcting this code, (saying what i have to do) after i will learn php, but now i need this code working fine. Thanks for all Quote Link to comment Share on other sites More sharing options...
xyph Posted July 23, 2012 Share Posted July 23, 2012 So i have to put this code where? I think, you need to start learning php from beginning - step by step . But you can only help me correcting this code, (saying what i have to do) after i will learn php, but now i need this code working fine. Thanks for all We are here to guide, not do for you. Quote Link to comment Share on other sites More sharing options...
pes96 Posted July 23, 2012 Author Share Posted July 23, 2012 So someone can guide me to what i have to do? Quote Link to comment Share on other sites More sharing options...
jazzman1 Posted July 23, 2012 Share Posted July 23, 2012 Go back to post #13. Quote Link to comment Share on other sites More sharing options...
pes96 Posted July 23, 2012 Author Share Posted July 23, 2012 So you can explain me where is the error in my code? Thanks for all My apologies, your sql syntax is correct :-\ First, I thought that the line 28 is just before the query I gave you above. Secondly, you can make a simple debugging test to check, what actually values you will send to database. <?php $username = 'jazzman'; $password = 'password'; $id = 1; $sql1 = "SELECT * FROM user_accounts WHERE username='".$username."' AND password='".$password."' AND user_id='".$id."'"; $sql2 = 'SELECT * FROM user_accounts WHERE username ='.$username.' AND password='.$password.' AND user_id='.$id; // integer id $sql3 = "SELECT * FROM user_accounts WHERE username='$username' AND password='$password' AND user_id='$id'"; $sql4 = 'SELECT * FROM user_accounts WHERE username=$username AND password=$password'; // invalid query printf('%s<br />%s<br />%s <br />%s', $sql1, $sql2, $sql3, $sql4); So and now? Thanks for your patience Quote Link to comment Share on other sites More sharing options...
fenway Posted July 25, 2012 Share Posted July 25, 2012 If you can't even prove that you can connect the database, there's really nothing we can do. 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.