j.smith1981 Posted February 21, 2011 Share Posted February 21, 2011 I am having real problems getting mysql_num_rows function working with PHP 5 This is the split up script: index.php <?php ini_set('display_errors', 1); require_once 'config.php'; require_once 'func/func.db.php'; $connect = databaseConnect($host, $user, $password, $database); if($connect == true) { // printf("<table>"); // get it querying results for showing users entries as of now: $result = query("SELECT * FROM entries"); if($result == true) { printf("<table> <tr> <td></td> </tr>\n"); while($row = mysql_fetch_array($result)) { printf(" <tr> <td>Result row</td> </tr>\n"); } printf("</table>\n\n"); } // show form for users logged in so they can post entries, ie just one user account for now: require_once 'auth.php'; } ?> func/func.db.php <?php function databaseConnect($host, $user, $password, $database) { $connect = mysql_connect($host, $user, $password); if($connect) { $select_db = mysql_select_db($database, $connect); if($select_db) { return true; } else { return false; } } else { return false; } } function query($sql) { $sql = stripslashes($sql); $query = mysql_real_escape_string($sql); $result = mysql_query($query); if($result){ return $result; } } Then the auth.php, looks like: <?php if(isset($_POST)) { $message = 'Only registered users can post'; if(array_key_exists('submit',$_POST)) { if($_POST['username'] != '' && $_POST['password'] != '') { $username = $_POST['username']; $password = sha1($_POST['password']); // try to log user in: $result = query("SELECT * FROM users WHERE username = '$username' AND '$password'"); echo mysql_num_rows($result); } else { $message = 'You did not enter all required fields, please retry'; } } } else { $message = 'Only registered users can post'; } ?> <p><?=$message;?></p> <table> <form id="login" name="login" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <tr> <td><label for="username">User: </label></td> <td><input type="text" id="username" name="username" value="" /></td> </tr> <tr> <td><label for="password">Password: </label></td> <td><input type="password" id="password" name="password" value="" /></td> </tr> <tr> <td><input type="submit" id="submit" name="submit" value="Login" /></td> </tr> </form> <table> You dont need to know whats in config.php since thats just full of my database connection variables, like the hostname, user, password and database, but its saying mysql_num_rows is an invalid function, since maybe its because its lost connection to the database, but how would I get around this? I thought about maybe making it connect to the database again, but is there any point in doing this? This is the actual error I get: Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /www/jeremysmith.me.uk/html/blog-dev/auth.php on line 17 Any help is much appreciated, Jez. Quote Link to comment https://forums.phpfreaks.com/topic/228376-mysql_num_rows-problem-can-someone-help/ Share on other sites More sharing options...
Muddy_Funster Posted February 21, 2011 Share Posted February 21, 2011 that error has nothing to do with the mysql_num_rows function being unrecognised. That error is thrown because your SQL syntax is malformed in the following: $result = query("SELECT * FROM users WHERE username = '$username' AND '$password'"); The fact that you are using SELECT * asside, you are not checking $password against a field name in the database. Try: $result = query("SELECT username FROM users WHERE username = '$username' AND password= '$password'"); Quote Link to comment https://forums.phpfreaks.com/topic/228376-mysql_num_rows-problem-can-someone-help/#findComment-1177563 Share on other sites More sharing options...
j.smith1981 Posted February 21, 2011 Author Share Posted February 21, 2011 Nope its still causing problems even though I have fixed it, didnt think that'd work. It would still come up with that error even when I have actually noticed my own error, but it wouldnt matter anyways, its PHP saying that its an unrecognised resource, so no matter what syntax you use, it would still come up with that error, its bringing that error up even before the sql syntax is executed, I just cant remember how to get around it, thats all. It's breaking before even executing whats in the SQL Could even have "SELECT *" and that would bring up the exact same error. Can someone help please? Quote Link to comment https://forums.phpfreaks.com/topic/228376-mysql_num_rows-problem-can-someone-help/#findComment-1177565 Share on other sites More sharing options...
Muddy_Funster Posted February 21, 2011 Share Posted February 21, 2011 Every time I have come accross that message (and other simmilar ones) it has been an SQL issue (wrong table selected, case sensitive errors, etc.), not a PHP one. That your case may be different is indeed possable, I was just playing the numbers on that one. Hopefully someone else will be able to help. Quote Link to comment https://forums.phpfreaks.com/topic/228376-mysql_num_rows-problem-can-someone-help/#findComment-1177574 Share on other sites More sharing options...
j.smith1981 Posted February 21, 2011 Author Share Posted February 21, 2011 Ah I know now. sorry yes. But if I change the query line to: $result = mysql_query("SELECT * FROM users WHERE username = '$username' and password = '$password'"); if(!$result) { echo mysql_error(); } else { echo $result; } It works, just outputted the SQL statement before and its not removing the slashes which is what stripslashes is supposed to do, any suggestions? Quote Link to comment https://forums.phpfreaks.com/topic/228376-mysql_num_rows-problem-can-someone-help/#findComment-1177576 Share on other sites More sharing options...
j.smith1981 Posted February 21, 2011 Author Share Posted February 21, 2011 I know what I have done! Put it within an if statement, this is how I did it: $result = query("SELECT * FROM users WHERE username = '$username' and password = sha1('$password')"); $count = mysql_num_rows($result); Now it works, don't really see what I have done to get around this (probs due to the number of times I have looked at it), but it works now. Thanks, Jez. Quote Link to comment https://forums.phpfreaks.com/topic/228376-mysql_num_rows-problem-can-someone-help/#findComment-1177616 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.