Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

mysql_num_rows() not working?

Featured Replies

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");

?>

Try changing to

$num_rows = mysql_num_rows();

The function should pick up the last query result object automatically.

$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.

  • 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?

  • 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

  • Author

Just started working, as Gibson said, needed to get rid of the quotes, the rest of the code was fine.

 

Cheers guys.

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

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.