By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,142 Members | 1,056 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,142 IT Pros & Developers. It's quick & easy.

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in

P: 3
When i run this code, i keep getting this message;

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\wamp\www\checklogin.php on line 26

i use this code to check login details. Here is the code for the pages that i used.

main_login.php

[PHP]<html>
<body>
<table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form name="form1" method="post" action="checklogin.php">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td colspan="3"><strong>Member Login </strong></td>
</tr>
<tr>
<td width="78">Username</td>
<td width="6">:</td>
<td width="294"><input name="myusername" type="text" id="myusername"></td>
</tr>
<tr>
<td>Password</td>
<td>:</td>
<td><input name="mypassword" type="text" id="mypassword"></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td><input type="submit" name="Submit" value="Login"></td>
</tr>
</table>
</td>
</form>
</tr>
</table>
</body>
</html>[/PHP]

checklogin.php

[PHP]<?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 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 "Wrong Username or Password";
}
?>[/PHP]

login_success.php

[PHP]<?
session_start();
if(!session_is_registered(myusername)){
header("location:main_login.php");
}
?>

<html>
<body>
Login Successful
</body>
</html>[/PHP]

could anyone see the error on the check login page.
Nov 15 '08 #1
Share this Question
Share on Google+
4 Replies


Markus
Expert 5K+
P: 6,050
Your mysql_result() is probably generating an error or returning false (empty).

Change line 23 (checklogin.php) to
Expand|Select|Wrap|Line Numbers
  1. $result=mysql_query($sql);
and see if you get an error.
Nov 15 '08 #2

P: 3
Your mysql_result() is probably generating an error or returning false (empty).

Change line 23 (checklogin.php) to
Expand|Select|Wrap|Line Numbers
  1. $result=mysql_query($sql);
and see if you get an error.
i replaced that line of code and i still get an error. is there any solution
Sorry, my brain must've slipped.
Expand|Select|Wrap|Line Numbers
  1. $result=mysql_query($sql) or die(mysql_error());
is correct
Nov 16 '08 #3

P: 3
Sorry, my brain must've slipped.
Expand|Select|Wrap|Line Numbers
  1. $result=mysql_query($sql) or die(mysql_error());
is correct
thank for the help. i am really greatful
Nov 16 '08 #4

Markus
Expert 5K+
P: 6,050
thank for the help. i am really greatful
Do you get an error when you add this?
Nov 16 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.