...

View Full Version : can you see where my code is wrong?



calsta
04-28-2012, 11:37 AM
hi guys i am making a log in form and i have noticed that when i come to use the form i get an error. Here is the code and the error i receive, do you have any ideas on how i can fix this? thanks calsta

<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="test"; // Database name
$tbl_name="members"; // Table name

// Connect to server and select database.
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
$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);

$count=mysql_num_rows($result);

if($count==1){
session_register("myusername");
session_register("mypassword");
header("location:login_success.php");
}
else {
echo "Wrong Username or Password";
}
?>

error is: Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\wamp\www\Login\check_login.php on line 26

abduraooft
04-28-2012, 11:48 AM
Change
$result=mysql_query($sql); to

$result=mysql_query($sql) or die(mysql_error()); to get the error in your query.

calsta
04-28-2012, 11:58 AM
Change
$result=mysql_query($sql); to

$result=mysql_query($sql) or die(mysql_error()); to get the error in your query.

Thanks for that i help me find out that the table it is trying to access does not exist but i don't see the part where it is trying to access test.members here is the code with the part you added in

<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="test"; // Database name
$tbl_name="members"; // Table name

// Connect to server and select database.
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
$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) or die(mysql_error());

$count=mysql_num_rows($result);

if($count==1){
session_register("myusername");
session_register("mypassword");
header("location:login_success.php");
}
else {
echo "Wrong Username or Password";
}
?>
from this i don't understand why it is connecting to test.members instead of just test, any ideas?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum