Connecting Tech Pros Worldwide Forums | Help | Site Map

PHP and mysql: Login query??

Newbie
 
Join Date: Mar 2008
Posts: 11
#1: Nov 18 '08
Expand|Select|Wrap|Line Numbers
  1. function log_in($uname,$pword){
  2.         if (mysql_num_rows(mysql_query("SELECT `iduser`, `cardno` FROM `user` WHERE `uname` = 'Steffan_James' AND `pword` = 'collingwoodcard'"))){
  3.         $res = mysql_query("SELECT * FROM `user` WHERE `uname` = 'Steffan_James' AND `pword` = 'collingwoodcard' LIMIT 1");
  4.         $row = mysql_fetch_array($res);
  5.         setcookie('id',$row["iduser"],2592000 + time(),'/');
  6.         setcookie('cno',$row["cardno"],2592000 + time(),'/');
  7.         return 1;
  8.         } else {return 0;}
  9. }
That's it. I've filled the query with some test details, and i know that my passwords are insecure.

It won't log in, even though the query runs fine on PHPMyAdmin...

Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,751
#2: Nov 18 '08

re: PHP and mysql: Login query??


This code is very inefficient. You are executing the query twice, when doing it once would be perfectly fine. Not to mention that the second time you are getting all the fields, not just those you are using.

Try doing something more like:
Expand|Select|Wrap|Line Numbers
  1. function doLogin()
  2. {
  3.   $sql = "SELECT id, whatever FROM user WHERE uname = 'name' AND pwd = 'pass'";
  4.   $res = mysql_query($sql) or die("Login query failed: ". mysql_error());
  5.   if(mysql_num_rows($res)) {
  6.     // Set cookies and stuff here...
  7.     return true;
  8.   }
  9.   else {
  10.     return false;
  11.   }
  12. }
  13.  
Try that and see if it helps any.
Newbie
 
Join Date: Mar 2008
Posts: 11
#3: Nov 18 '08

re: PHP and mysql: Login query??


I realize what went wrong now... I FORGOT TO CONNECT TO THE DATABASE. Thanks for your help.
Reply