It is currently Fri Mar 29, 2024 10:41 am


All times are UTC


Forum rules


Please click here to view the forum rules



Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: it seems like mysql_query() always return resource id #2
PostPosted: Sun Apr 23, 2006 5:05 am 
Noobie
Noobie

Joined: Tue Apr 18, 2006 8:08 am
Posts: 7
Location: Philippines
It seems like mysql_query() always return resource id #2 even if my table do not contain record specified in where clause. as long as i remember, if there's no result of query, mysql_query() returns false. Please help with this...

my query is this
$query = "select uid from users where username = '$user'";

this is my script:

<html>
<head></head>

<body>
<?php

if (!$_POST['submit'])
{
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" name="f1" method="post">
<table width="100%">
<tr><td>USERNAME</td><td><input type="text" name="username" size="10"</td></tr>
<tr><td>PASSWORD</td><td><input type="password" name="password" size="10"</td></tr>
<tr><td><input type="submit" name="submit" value="Sign In"</td></tr>
</table>
</form>
<?php
}
else
{
$errorlist = array();
$user = $_POST['username'];
$pass = $_POST['password'];
if (trim($_POST['username']) == '')
{
$errorlist[] = 'invalid username';
}
if (trim($_POST['password']) == '')
{
$errorlist[] = 'invalid password';
}
if (sizeof($errorlist) > 0)
{
foreach ($errorlist as $list)
{
echo "<li>$list";
}
}
else
{
echo "Good <br>";
echo "username $user <br>";
echo "password $pass";

$db_user = "xxxxxx";
$db_host = "xxxxxx";
$db_pass = "xxxxxxx";
$db = "xxxxxxx";

$conn = mysql_connect($db_host,$db_user,$db_pass);
mysql_select_db($db);
$query = "select uid from users where username = '$user'";
$result = mysql_query($query,$conn);

echo $result;

// if (!$result)
// {
// echo "No Account Found!";
// }
// else
// {
// $row = mysql_fetch_array($result);
// $uid = $row['uid'];
// echo "User id is: $uid";

// }

}
}


?>


</body>
</html>

_________________
Jeboy


Top
 Profile  
 
 Post subject:
PostPosted: Sun Apr 23, 2006 12:12 pm 
Posts like crazy!
Posts like crazy!

Joined: Mon Sep 26, 2005 5:03 pm
Posts: 204
Location: The Netherlands
You could use mysql_num_rows()

EDIT:

I fixed your code, or else you would keep posting. :P
It is tested.. and it worked for me.. I tried to stay as close to your script as possible. But I found it quite odd that there is a username check but no password check.. ?

Code:
<html>
<head>
</head>
<body>
<?php

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

?>
<form action="" name="f1" method="post">
<table width="100%">
<tr>
   <td>USERNAME</td>
   <td><input type="text" name="username" size="10" /></td>
</tr>
<tr>
   <td>PASSWORD</td>
   <td><input type="password" name="password" size="10" /></td>
</tr>
<tr>
   <td colspan="2"><input type="submit" name="submit" value="Sign In" /></td>
</tr>
</table>
</form>
<?php

} else {
   $errorlist = array();
   $user = $_POST['username'];
   $pass = $_POST['password'];

   if (trim($user) == '')
      $errorlist[] = 'invalid username';

   if (trim($pass) == '')
      $errorlist[] = 'invalid password';

   if (sizeof($errorlist) > 0) {
      foreach ($errorlist as $list) {
         echo "<li>$list";
      }
   } else {
      echo "Good <br />";
      echo "username $user <br />";
      echo "password $pass <br />";

      $db_user = "xxxx";
      $db_host = "xxxx";
      $db_pass = "xxxx";
      $db = "xxxx";

      $conn = mysql_connect($db_host,$db_user,$db_pass);
      mysql_select_db($db);

      $query = "SELECT uid FROM users WHERE username = '$user'";
      $result = mysql_query($query);

      if (mysql_num_rows($result) > 0) {
         $row = mysql_fetch_assoc($result);
         echo "User id is: ".$row['uid'];
      } else {
         echo "No Account Found!";
      }
   }
}

?>
</body>
</html>


Top
 Profile  
 
 Post subject: Thanks a lot!
PostPosted: Wed Apr 26, 2006 6:31 am 
Noobie
Noobie

Joined: Tue Apr 18, 2006 8:08 am
Posts: 7
Location: Philippines
Thank you very much... Your great!

I beg your indulgence, im only a 2nd year bs comsci student.

_________________
Jeboy


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ]  Moderators: fhmagic, KJ, Moderators, Support Team

All times are UTC


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
100WebSpace © 2011