poliosynthesis Posted August 19, 2013 Share Posted August 19, 2013 Hi Guys, I have a simple mysql_num_rows() expects parameter 1 to be resource, boolean given error in my script, I have tried to debug it myself but I don't understand why the script doesn't think the variable "$query" isn't an integer. I am trying to create a login form for users who are already registered and I want them to be able to see instant feedback as to whether their info has been accepted or not. The users won't be redirected though. testlogin.php <?php $name = $_GET['name']; $password = $_GET['password']; if (!$name && $password) { echo "Error"; exit; } mysql_connect("localhost" , "root" , "") or die("Issue with connection!"); mysql_select_db("testlogin"); $query = mysql_query("SELECT * FROM users WHERE Name='".$name."'"); $name = $_GET['name']; $password = $_GET['password']; if(!$name && $password) { echo 'No name or password'; exit(); } mysql_connect("localhost","root", ""); mysql_select_db("testlogin"); $query = mysql_query("SELECT * FROM users WHERE Name ='".$name."'"); $numrows = mysql_num_rows($query); if($numrows !=0) { while($row = mysql_fetch_assoc($query)) { $dbname = $row['Username']; $dbpassword = $row['password']; } if($name == $dbname && $password == $dbpassword) { echo "you are in!"; }else { echo "Please enter a valid username and password"; } }else { echo "Your name is not registered!"; } ?> Link to comment https://forums.phpfreaks.com/topic/281357-mysql_num_rows-expects-parameter-1-to-be-resource-boolean-given/ Share on other sites More sharing options...
poliosynthesis Posted August 19, 2013 Author Share Posted August 19, 2013 Sorry Guys, I doubled up with my mysql_connects etc. I have since removed that, it is not the error. <?php $name = $_GET['name']; $password = $_GET['password']; if (!$name && $password) { echo "Error"; exit; } mysql_connect("localhost" , "root" , "") or die("Issue with connection!"); mysql_select_db("testlogin"); $query = "SELECT * FROM users WHERE Name='".$name."'"; $result = mysql_query($query); $numrows = mysql_num_rows($query); if($numrows !=0) { while($row = mysql_fetch_assoc($query)) { $dbname = $row['Username']; $dbpassword = $row['password']; } if($name == $dbname && $password == $dbpassword) { echo "you are in!"; }else { echo "Please enter a valid username and password"; } }else { echo "Your name is not registered!"; } ?> Link to comment https://forums.phpfreaks.com/topic/281357-mysql_num_rows-expects-parameter-1-to-be-resource-boolean-given/#findComment-1445796 Share on other sites More sharing options...
mac_gyver Posted August 19, 2013 Share Posted August 19, 2013 are you sure that's your actual code that you need help with? $query in the last posted code is your sql statement. $result is the value from the mysql_query(). there's no way that mysql_num_rows($query) or mysql_fetch_assoc($query) will work. you should be copy/pasting your actual code so that there isn't a run-a-round about what you are actually asking help for. a SELECT query that runs without any errors, returns a result resource that you can then use in other database statements. a SELECT query that fails due to an error (connection problem, permissions problem. table/column name problems, sql syntax problem, ...) returns a boolean false value. you must always test if a step in your code works or fails before trying to use the result from that step. to debug why your query is failing (assuming you are using all the correct variable names in the right places), use the following when running your queries - $result = mysql_query($query) or die("Query failed: $query<br>Error: " . mysql_error()); Link to comment https://forums.phpfreaks.com/topic/281357-mysql_num_rows-expects-parameter-1-to-be-resource-boolean-given/#findComment-1445800 Share on other sites More sharing options...
davidannis Posted August 19, 2013 Share Posted August 19, 2013 I think that mac_gyver has found the problem. You are using the wrong variable in: $numrows = mysql_num_rows($query); it should read $numrows = mysql_num_rows($result); Link to comment https://forums.phpfreaks.com/topic/281357-mysql_num_rows-expects-parameter-1-to-be-resource-boolean-given/#findComment-1445801 Share on other sites More sharing options...
poliosynthesis Posted August 20, 2013 Author Share Posted August 20, 2013 Hi Guys, I am very sorry to have wasted your time, it was an error based on an incorrect mysql_query I entered the wrong name of the row. If you guys would like me to post my code for future reference for others with my problem I will. Sorry again to have wasted your time. Thanks for the help you did give me though Link to comment https://forums.phpfreaks.com/topic/281357-mysql_num_rows-expects-parameter-1-to-be-resource-boolean-given/#findComment-1445946 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.