Jump to content

mysql_num_rows() expects parameter 1 to be resource,


VikiC

Recommended Posts

Hi

 

I'm having a bit of bother with my login.  I created a login using this tutorial  http://www.phpeasystep.com/phptu/6.html and it works perfectly. 

 

So i have attempted to change it to meet my own database. So basically i've changed the database, table names etc to meet my own. I haven't changed any other lines. When i run it i get an error message:

 

Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\checklogin.php on line 26

 

The code is below:

 

<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="final year project"; // Database name
$tbl_name="tbl_user"; // Table name

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

// username and password sent from form
$mem_username=$_POST['mem_username'];
$mem_password=$_POST['mem_password'];

// To protect MySQL injection (more detail about MySQL injection)
$mem_username = stripslashes($mem_username);
$mem_password = stripslashes($mem_password);
$mem_username = mysql_real_escape_string($mem_username);
$mem_password = mysql_real_escape_string($mem_password);

$sql="SELECT * FROM $tbl_name WHERE username='$mem_username' and password='$mem_password'";
$result=mysql_query($sql);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $mem_username and $mem_password, table row must be 1 row

if($count==1){
// Register $mem_username, $mem_password and redirect to file "login_success.php"
session_register("mem_username");
session_register("mem_password");
header("location:login_success.php");
}
else {
echo "Wrong Username or Password";
}
?>

 

Line 26 is $count=mysql_num_rows($result);

 

 

I'm baffled as to why the test database worked. I tried another test database but got the same error. baffled.com

 

Hope someone can help :)

 

MOD EDIT:

 . . . 

tags added.

I now get:

 

Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\checklogin.php on line 26

 

Fatal error: SQL: SELECT * FROM tbl_user WHERE username='' and password='', Error: Unknown column 'username' in 'where clause' in C:\xampp\htdocs\checklogin.php on line 26

breakin' it down....

 

Fatal error: SQL: SELECT * FROM tbl_user WHERE username='' and password='', Error: Unknown column 'username' in 'where clause' in C:\xampp\htdocs\checklogin.php on line 26

 

SQL: SELECT * FROM tbl_user WHERE username='' and password=''

 

Error: Unknown column 'username' in 'where clause'

 

Unknown column 'username'

Unfortunately, that tutorial sucks. It's outdated, uses deprecated functions, and makes the assumption that magic_quotes_gpc = On without even testing for it. It appears to be written for PHP 4. Forget most of what you learned from it, post your form here, and I'll have a look at what's happening.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.