July 6, 201115 yr I started making this code by looking at other scripts. The code isn't finished but this is where I got to before I got an error. The code is for someone to log into a members area. When I came to test the $num_rows variable, instead of getting 1 or 0 I get an error saying Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/a5734181/public_html/php/login_details.php on line 23 I've looked on the internet, lots of people seem to have this issue but I haven't really found an answer as to why this doesn't work just how to make it different, I've seen it work before. This is my code so far, thanks guys. <?php // email and password sent from form $email = $_POST['email']; $password = $_POST['password']; if ($email&&$password) { $connect = mysql_connect("mysql7.######.com","hostusername","hostpassword") or die("Couldn't connect"); mysql_select_db("a5734181_omcdb") or die("Couldn't find db"); $query = mysql_query("SELECT * FROM login_test WHERE Email = '$email' AND Password = '$password' "); $num_rows = mysql_num_rows("$query"); echo $num_rows; } else die("Please enter Email and Password"); ?>
July 6, 201115 yr Try changing to $num_rows = mysql_num_rows(); The function should pick up the last query result object automatically.
July 6, 201115 yr $num_rows = mysql_num_rows("$query"); For a start you have quotes round $query. Get rid of those. Then, you have to actually execute the query first to get the number of rows, so you'd have to do $result = mysql_query($query); $num_rows = mysql_num_rows($result); Or similar.
July 6, 201115 yr Author Try changing to $num_rows = mysql_num_rows(); The function should pick up the last query result object automatically. Nope, I get the same error. Would it be something in the PHP configuration that might need changing?
July 6, 201115 yr Author For a start you have quotes round $query. Get rid of those. Then, you have to actually execute the query first to get the number of rows, so you'd have to do $result = mysql_query($query); $num_rows = mysql_num_rows($result); Or similar. That still generates the same result
July 6, 201115 yr Author Just started working, as Gibson said, needed to get rid of the quotes, the rest of the code was fine. Cheers guys.
July 6, 201115 yr That still generates the same result Sorry, slightly misread your code in the first post. I misread it as $query = "SELECT * FROM login_test WHERE Email = '$email' AND Password = '$password' "; when in actual fact you'd already put the mysql_query bit in there, that's why it generated the same result and didn't need my $result = mysql_query($query) bit. Sometimes I do it like that - I know it's one extra line of code, but it makes it easier to read and you can just echo your query as a quick solution debug method. By the way - have you investigated PDO? I recommend you read up on it. The prepared statements may interest you.
Create an account or sign in to comment