mysql_num_rows warning

Reply

Join Date: Sep 2006
Posts: 6
Reputation: jyan is an unknown quantity at this point 
Solved Threads: 0
jyan jyan is offline Offline
Newbie Poster

mysql_num_rows warning

 
0
  #1
Sep 25th, 2006
Hi!

I'm doing a small website with some mysql and php, and have difficulties with doing a login site. I have the form sending the username and password, and this site should check them.

Here's the code:

[php]

<?php

session_start();

if(!($connection = mysql_connect("host","user","pass")))
die("Can't connect to database.");
if(!(mysql_select_db("db", $connection)))
die("ERROR ".mysql_errno($connection)."-". mysql_error($connection));

if(isset($_POST["username"]) AND isset($_POST["password"])) {
$uname = $_POST["username"];
$pword = $_POST["password"];


$loginquery = "SELECT * FROM Users WHERE Username = '{$uname}' AND Password = '{$pword}' ORDER BY UserID";
if(!$loginresult = mysql_query($loginquery, $connection))
$error = "No connection.";

if (mysql_num_rows($loginresult) != 1){
$error .= "Username / password error!";
}
else {
$datas = mysql_fetch_assoc($loginresult);
$uid2 = $datas["UserID"];
$uname2 = $datas["Username"];
$pword2 = $datas["Userpassword"];
$artist2 = $datas["Artist"];
$moderator2 = $datas["Moderator"];

$_SESSION["userid"] = $uid2;
$_SESSION["username"] = $uname2;
$_SESSION["password"] = $pword2;
$_SESSION["artist"] = $artist2;
$_SESSION["moderator"] = $moderator2;
}

}
?>
[/php]
This php page is included in every page on the site. Some variables may be named badly, ignore them. Just help me, please?
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 84
Reputation: rinoa04 is on a distinguished road 
Solved Threads: 4
rinoa04's Avatar
rinoa04 rinoa04 is offline Offline
Junior Poster in Training

Re: mysql_num_rows warning

 
0
  #2
Sep 26th, 2006
[php]

$loginquery = "SELECT * FROM Users WHERE Username = '{$uname}' AND Password = '{$pword}' ORDER BY UserID";
if(!$loginresult = mysql_query($loginquery, $connection))
$error = "No connection.";

if (mysql_num_rows($loginresult) != 1){
$error .= "Username / password error!";
}
[/php]

You can try to modify the coding above as below. See whether it works or not.

[php]
$loginquery = mysql_query("SELECT * FROM Users WHERE Username = '$uname' AND Password = '$pword' ORDER BY UserID");

if (!$loginquery){$error="Query problem";}

if (mysql_num_rows($loginquery) != 1){
$error .= "Username / password error!";
}
[/php]
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 6
Reputation: jyan is an unknown quantity at this point 
Solved Threads: 0
jyan jyan is offline Offline
Newbie Poster

Re: mysql_num_rows warning

 
0
  #3
Sep 26th, 2006
No, didn't help. Thanks for trying though.
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 6
Reputation: jyan is an unknown quantity at this point 
Solved Threads: 0
jyan jyan is offline Offline
Newbie Poster

Re: mysql_num_rows warning

 
0
  #4
Sep 28th, 2006
Still not working. Does anyone have any idea what the problem is? I really should get this done quite soon and thought it would be easy, since I've done a similar site previously and had no problems... Pretty please?
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 6
Reputation: jyan is an unknown quantity at this point 
Solved Threads: 0
jyan jyan is offline Offline
Newbie Poster

Re: mysql_num_rows warning

 
0
  #5
Sep 28th, 2006
FIXED IT! :mrgreen:

Or actually didn't fix it, just did something else. Here's the code if someone's wondering.

Before:

[php]
$loginquery = "SELECT * FROM Users WHERE Username = '{$uname}' AND Password = '{$pword}' ORDER BY UserID";
if(!$loginresult = mysql_query($loginquery, $connection))
$error = "No connection.";

if (mysql_num_rows($loginresult) != 1){
$error .= "Username / password error!";
}
else {
$datas = mysql_fetch_assoc($loginresult);
$uid2 = $datas["UserID"];
$uname2 = $datas["Username"];
$pword2 = $datas["Userpassword"];
$artist2 = $datas["Artist"];
$moderator2 = $datas["Moderator"];

$_SESSION["userid"] = $uid2;
$_SESSION["username"] = $uname2;
$_SESSION["password"] = $pword2;
$_SESSION["artist"] = $artist2;
$_SESSION["moderator"] = $moderator2;
}
[/php]
After:
[php]
$loginquery = "SELECT * FROM Users ORDER BY UserID";
$loginresult = mysql_query($loginquery);

while($row = mysql_fetch_assoc($loginresult)){
if($row[Username]==$uname && $row[Userpassword]==$pword){
$_SESSION["userid"] = $row[UserID];
$_SESSION["username"] = $row[Username];
$_SESSION["password"] = $row[Userpassword];
$_SESSION["artist"] = $row[Artist];
$_SESSION["moderator"] = $row[Moderator];
}
}
[/php]
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 16
Reputation: sunflowerz is an unknown quantity at this point 
Solved Threads: 0
sunflowerz's Avatar
sunflowerz sunflowerz is offline Offline
Newbie Poster

Re: mysql_num_rows warning

 
0
  #6
Sep 30th, 2006
Instead of:
[php]
$loginquery = "SELECT * FROM Users WHERE Username = '{$uname}' AND Password = '{$pword}' ORDER BY UserID";

if(!$loginresult = mysql_query($loginquery, $connection))
$error = "No connection.";

if (mysql_num_rows($loginresult) != 1){
$error .= "Username / password error!";
}[/php]

Try this:

[php]
$loginquery = mysql_query("SELECT * FROM Users WHERE Username = '".$uname."' AND Password = '".$pword."' ORDER BY UserID");

if (!$loginquery){$error="Query problem";}

if (mysql_num_rows($loginquery) != 1){
$error .= "Username / password error!";
}[/php]

Or, change the !=1 to <1 as there shouldn't be more than two usernames that are the same.

The other way is to go through phpMyAdmin and paste it into SQL Query. It (usually) tells you the why it won't work, or at least where it thinks the error is.

Thanks for putting up your work around.
Last edited by sunflowerz; Sep 30th, 2006 at 3:03 am. Reason: code blocks didn't quite work...oops! :)
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 1
Reputation: itanium7000 is an unknown quantity at this point 
Solved Threads: 0
itanium7000 itanium7000 is offline Offline
Newbie Poster

Re: mysql_num_rows warning

 
0
  #7
Sep 30th, 2006
...and why using AND operator, not use && operator?
Đường đời sóng gió anh không sợ
Chỉ sợ ngày về thiếu bóng em
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 6
Reputation: jyan is an unknown quantity at this point 
Solved Threads: 0
jyan jyan is offline Offline
Newbie Poster

Re: mysql_num_rows warning

 
0
  #8
Sep 30th, 2006
Originally Posted by itanium7000 View Post
...and why using AND operator, not use && operator?
Is there a difference?
Reply With Quote Quick reply to this message  
Join Date: Jul 2004
Posts: 494
Reputation: Puckdropper is an unknown quantity at this point 
Solved Threads: 21
Puckdropper Puckdropper is offline Offline
Posting Pro in Training

Re: mysql_num_rows warning

 
0
  #9
Oct 1st, 2006
Yes, && is evaluated first.

From: http://us2.php.net/manual/en/language.operators.php
09-Jun-2004 05:58
of course this should be clear, but i think it has to be mentioned espacially:

AND is not the same like &&

for example:

<?php $a && $b || $c; ?>
is not the same like
<?php $a AND $b || $c; ?>

the first thing is
(a and b) or c

the second
a and (b or c)

'cause || has got a higher priority than and, but less than &&

of course, using always [ && and || ] or [ AND and OR ] would be okay, but than you should at least respect the following:

<?php $a = $b && $c; ?>
<?php $a = $b AND $c; ?>

the first code will set $a to the result of the comparison $b with $c, both have to be true, while the second code line will set $a like $b and THAN - after that - compare the success of this with the value of $c

maybe usefull for some tricky coding and helpfull to prevent bugs

greetz, Warhog
The next comment also points out that relying on order of operations is a good way to produce bugs and errors.
www.uncreativelabs.net

Old computers are getting to be a lost art. Here at Uncreative Labs, we still enjoy using the old computers. Sometimes we want to see how far a particular system can go, other times we use a stock system to remind ourselves of what we once had.
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 133
Reputation: sn4rf3r is an unknown quantity at this point 
Solved Threads: 2
sn4rf3r's Avatar
sn4rf3r sn4rf3r is offline Offline
Junior Poster

Re: mysql_num_rows warning

 
0
  #10
Oct 2nd, 2006
Just a quick thought, you are not filtering any input from the login form. If I happened to put my user name as (delete * from users where 1), all of your user information would dissappear.

I suggest that you run your input data through at least htmlentities, this would convert the perens and astrix to their ascii equivilent.

Also, just semantics, you could put your data into an array and then store that array in the $_SESSION
if (isset($_POST['username']) && !empty($_POST['password'])) {
   $user = array(
      'uname' = htmlentities($_POST['username']),
      'pswd' = htmlentities($_POST['password'])
);
$sql = "SELECT * FROM users where username = '".$user['uname']."' AND password = '".$user['pswd]."'";
$loginquery = mysql_query($sql,$connection);
//row count conditional goes here

while ($row = mysql_fetch_assoc($loginquery)) {
if($row[Username]==$uname && $row[Userpassword]==$pword){
   $user['userid'] = $row['Userid'];
   $user['artist'] = $row['Artist'];
   $user['moderator'] = $row['moderator'];

$_SESSION['user'] = $user;
 }
}

Just one of many ways to do this.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the PHP Forum


Views: 3128 | Replies: 11
Thread Tools Search this Thread



Tag cloud for PHP
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC