Jump to content

PHP Login - Num_Rows


Calgaryalberta

Recommended Posts

Hey, I have a simple login script, and Im getting a

 

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in d:\hosting\member\aiim\login\checklogin.php on line 254

 

Line 254 is this:

$count=mysql_num_rows($result);

 

Here is the entire script, I dont see anything wrong with it :( - Any Suggestions?

 

<?php
$host="****"; // Host name 
$username="***"; // Mysql username 
$password="*****"; // Mysql password 
$db_name="****"; // Database name 
$tbl_name="****"; // 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 signup form 
$myusername=$_POST['myusername1']; 
$mypassword=$_POST['mypassword1']; 

$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("myusername1");
session_register("mypassword1"); 
header("location:login_success.php");
}
else {
echo "Wrong Username or Password";
}
?>

 

Link to comment
Share on other sites

Thats the warning which means that the $result parameter provided in the mysql_numrows(..) did not get the value to count the no of rows.Check that the query is executed properly.It seems that the query did not gave any result.

To remove the warning only put @ symbol infront of the called function:Ex

@mysql_numrows()............

Link to comment
Share on other sites

trie this way around....

<?php session_start();

$host="****"; // Host name 
$username="***"; // Mysql username 
$password="*****"; // Mysql password 
$db_name="****"; // Database name 
$tbl_name="****"; // 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 signup form 
$myusername=$_POST['myusername1']; 
$mypassword=$_POST['mypassword1']; 

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

// Mysql_num_row is counting table row

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

if(mysql_num_row($result)==1){

while($x=mysql_fetch_assoc($result)){

// Register $myusername, $mypassword and redirect to file "login_success.php"
$_SESSION["myusername1"]=$x['myusername1'];
$_SESSION["mypassword1=$x['mypassword1']; 
header("location:login_success.php");
}
else {
echo "Wrong Username or Password";
}
}
?>

Link to comment
Share on other sites

Try this:

 

<?php
$host="";         // Host name
$username = "";       // Mysql username
$password = "";           // Mysql password
$db_name  = "";   // Database name
$tbl_name = "";  // 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 signup form
$myusername = $_POST['myusername1'];
$mypassword = $_POST['mypassword1'];

$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("myusername1");
session_register("mypassword1");
header("location:login_success.php");

} else {
    echo "Wrong Username or Password";
}
?>

 

Also you might want to add some more security to that.

Link to comment
Share on other sites

THIS SHOULD WORK ASWELL I THINK TRY IT....

 

<?php session_start();

//database connection

$host="****"; // Host name 
$username="***"; // Mysql username 
$password="*****"; // Mysql password 
$db_name="****"; // Database name 
$tbl_name="****"; // 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");

//name off submit button is submit.......

if(isset($_POST['submit'])){

// username and password sent from signup form 

$myusername=$_POST['myusername1']; 
$mypassword=$_POST['mypassword1']; 

//select staement 

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

// Mysql_num_row is counting table row if 1 row exist

if(mysql_num_row($result)==1){

// Register $myusername, $mypassword and redirect to file "login_success.php"

$_SESSION['myusername1']=$myusername1;
$_SESSION['mypassword1']=$mypassword1; 

header("location:login_success.php");
exit;

}else{
// NOT A USER
echo" SORRY WE DONT HAVE YOU AS A SIGNUP USER PLEASE REGISTER";
}
}
?>

Link to comment
Share on other sites

For the last script posted, and I added a @mysql_num_row($result);

 

All parse errors are gone, but now it is saying

"Wrong Username/Password"

 

Problem is the username and password is right, :( I can't figure it out...Here is the new script

 

<?php
$host="****"; // Host name 
$username="****"; // Mysql username 
$password="****"; // Mysql password 
$db_name="***"; // Database name 
$tbl_name="****"; // 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 signup form
$myusername = $_POST['myusername1'];
$mypassword = $_POST['mypassword1'];

$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("myusername1");
session_register("mypassword1");
header("location:login_success.php");

} else {
   echo "Wrong Username or Password";
}
?>

Link to comment
Share on other sites

THIS SHOULD WORK ASWELL I THINK TRY IT....

 

Tried this code, got this error:

Fatal error: Call to undefined function: mysql_num_row() in d:\hosting\member\aiim\login\checklogin.php on line 257

Line 257 Reads:

if(mysql_num_row($result)==1){

 

<?php session_start();

//database connection

$host="****"; // Host name 
$username="***"; // Mysql username 
$password="*****"; // Mysql password 
$db_name="****"; // Database name 
$tbl_name="****"; // 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 signup form 

$myusername=$_POST['myusername1']; 
$mypassword=$_POST['mypassword1']; 

//select staement 

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

// Mysql_num_row is counting table row if 1 row exist

if(mysql_num_row($result)==1){

// Register $myusername, $mypassword and redirect to file "login_success.php"

$_SESSION['myusername1']=$myusername1;
$_SESSION['mypassword1']=$mypassword1; 

header("location:login_success.php");
exit;

}else{
// NOT A USER
echo" SORRY WE DONT HAVE YOU AS A SIGNUP USER PLEASE REGISTER";
}
?>

Link to comment
Share on other sites

code corrected make sure the form submit button is name="submit"

 

<?php session_start();

//database connection

$host="****"; // Host name 
$username="***"; // Mysql username 
$password="*****"; // Mysql password 
$db_name="****"; // Database name 
$tbl_name="****"; // 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");

//name off submit button is submit.......

if(isset($_POST['submit'])){

// username and password sent from signup form 

$myusername=$_POST['myusername1']; 
$mypassword=$_POST['mypassword1']; 

//select staement 

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

// Mysql_num_row is counting table row if 1 row exist

if(mysql_num_rows($result)==1){

// Register $myusername, $mypassword and redirect to file "login_success.php"

$_SESSION['myusername1']=$myusername1;
$_SESSION['mypassword1']=$mypassword1; 

header("location:login_success.php");
exit;

}else{
// NOT A USER
echo" SORRY WE DONT HAVE YOU AS A SIGNUP USER PLEASE REGISTER";
}
}
?>

Link to comment
Share on other sites

Tried the code above, seemed to work, except it didn't work. lol, the parse errors are gone, but with RedArrows code - the form submitted, but it said,

 

'Unknown column 'username' in Where Clause'

Problem is, the field is true, in the database  - the name of the column in the database is the same as it is on the script :( .

 

So not sure where the 'Unknown Column 'Username' in Where Clause' is coming from.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.