Warning: mysql_num_rows() expects parameter 1 to be resourc

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

jopereira
New php-forum User
New php-forum User
Posts: 1
Joined: Sat Mar 02, 2013 8:06 am

Warning: mysql_num_rows() expects parameter 1 to be resourc

Postby jopereira » Sat Mar 02, 2013 9:08 am

Code: Select all

<?php

$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="adad"; // Database name
$tbl_name="users"; // Table name

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("Não foi possível conectar à base de dados!");

// username and password sent from form
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];

// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);

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

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);

// If result matched $myusername and $mypassword, table row must be 1 row

if($count==1){

// Register $myusername, $mypassword and redirect to file "login_success.php"
session_register("myusername");
session_register("mypassword");
header("location:login_success.php");
}
else {
echo "Nome de utilizador ou senha incorrectos!";
}
?>


what's the problem with this?

the error line is 27:
$count=mysql_num_rows($result);

User avatar
simplypixie
php-forum Active User
php-forum Active User
Posts: 300
Joined: Sun Dec 11, 2011 12:51 am
Location: Shrewsbury, Shropshire
Contact:

Re: Warning: mysql_num_rows() expects parameter 1 to be reso

Postby simplypixie » Sun Mar 03, 2013 12:58 am

The error means that your query is failing (so there are no rows to count) so echo out your $sql and see what exact query it is trying to run on the database and make sure it is correct (if you think it looks correct then copy and paste into PHPMyAdmin and see what error you get there).


Return to “PHP coding => General”

Who is online

Users browsing this forum: No registered users and 2 guests