Incorrect Result with mysql_fetch_array

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
Caped Knight
Forum Commoner
Posts: 33
Joined: Wed Jan 01, 2003 6:20 pm
Location: Somewhere...out there...

Incorrect Result with mysql_fetch_array

Post by Caped Knight »

Ugh. This is just a script to test if a user exists (prelude to a login script). It's just been killing me for the past two days. I'm newbie to MySQL... Here's the script:

Code: Select all

<?php

mysql_connect("localhost","[user]","[password]") or die("Unable to connect to database.&nbsp&nbsp" . mysql_error() );
mysql_select_db("forums");

$query = "SELECT username FROM users WHERE username = '$username'";
$result = mysql_query($query) or die("Couldn't query.");
$row = mysql_fetch_array($result,MYSQL_ASSOC);

if($username == $row['username']){
    echo "User exists.";
}else{
    echo "User does not exist.";
}


include("signin.inc");

?>
signin.inc:

Code: Select all

&lt;html&gt;
&lt;head&gt;
       &lt;title&gt;Sign In&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;

&lt;form action="signin.php" method="post"&gt;
&lt;input type="text" name="username" maxlength="20"&gt;&lt;br&gt;
&lt;input type="submit" value="Sign In"&gt;
&lt;/form&gt;

&lt;/body&gt;
&lt;/html&gt;

...okay, now I start everything up and go to test it. Without even typing something in the form I get "User exists." Can anyone explain this to me?
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Try something like:

Code: Select all

<?php 
// test if a username has been entered
if (!empty($_POST['username'])) {

	$username = $_POST['username'];

	mysql_connect('localhost', '[user]', '[password]') or die('Unable to connect to database.&nbsp&nbsp' . mysql_error() ); 
	mysql_select_db('forums') or die(mysql_error()); 

	$query = "SELECT username FROM users WHERE username = '$username'"; 
	$result = mysql_query($query) or die(mysql_error().'<p>'.$query.'</p>'); 
	
	// test to see if there are any results
	if (mysql_num_rows($result) == 1) {
		echo 'User exists';
	} else {
		echo 'User does not exist.'; 
		include 'signin.inc'; 
	}
} else {
	echo 'No username was entered.';
	include 'signin.inc'; 
}

?>
Mac
Caped Knight
Forum Commoner
Posts: 33
Joined: Wed Jan 01, 2003 6:20 pm
Location: Somewhere...out there...

Post by Caped Knight »

Thanks! Just for one more annoying question, isn't typing "if($_POST['username')" the same as "if (!empty($_POST['username']))" in newer versions of PHP?
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

No they're not, if you do:

Code: Select all

if (!empty($_POST['username']) {
you are testing to see if the variable is set and that it is not equal to an empty string or zero, however, if you do

Code: Select all

if (!$_POST['username']) {
you are testing to see if the variable is equal to false - you should only use this second method for testing to see if variables are equal to true or false, not to test if they are set. To test if a variable is set use either empty() or isset(). If you turn your error reporting up to its highest level you will receive warning notices if you are using the second method.

Mac
Caped Knight
Forum Commoner
Posts: 33
Joined: Wed Jan 01, 2003 6:20 pm
Location: Somewhere...out there...

Post by Caped Knight »

Thanks for the help again. Now I've gotten an error that is putting me on the verge of having an anyeurism. This is the completed script. There are 44 lines in the script. I get this error: "Parse error: parse error in c:\apache\htdocs\forums2\signin.php on line 45"

Code: Select all

<?php

mysql_connect('localhost', '[user]', '[password]') or die('Unable to connect to database.&nbsp&nbsp' . mysql_error() );
mysql_select_db('forums') or die(mysql_error());

$username = $_POST['username'];
if(isset($username)){

   // Check username
   $query = "SELECT username FROM users WHERE username = '$username'";
   $result = mysql_query($query);

   if(mysql_num_rows($result) == 1){
      $user_exists = "TRUE";
   }else{
      $doesntExist = "Invalid user.";
      include 'signin.inc';
      die();
   }

   // Check password
   $password = $_POST['password'];
   if(isset($password)){

   $query = "SELECT * FROM users WHERE username = '$username' &&  password = '$password'";
   $result = mysql_query($query);

   if(mysql_num_rows($result) == 1){
      $correct_password = "TRUE";
   }else{
      $wrong_pass = "Incorrect password.";
      include 'signin.inc';
   }

}else{
   $notEntered = "No username was entered.";
   include 'signin.inc';
}

if($user_exists == "TRUE" && $correct_password == "TRUE"){
    echo "User name and password correct.";
}

?> <--line 44
User avatar
phice
Moderator
Posts: 1416
Joined: Sat Apr 20, 2002 3:14 pm
Location: Dallas, TX
Contact:

Post by phice »

You need to end your first if() statement.

Code:

Code: Select all

<?php

mysql_connect('localhost', '[user]', '[password]') or die('Unable to connect to database.&nbsp&nbsp' . mysql_error() ); 
mysql_select_db('forums') or die(mysql_error()); 

$username = $_POST['username']; 
if(isset($username)){ 

   // Check username 
   $query = "SELECT username FROM users WHERE username = '$username'"; 
   $result = mysql_query($query); 

   if(mysql_num_rows($result) == 1){ 
      $user_exists = "TRUE"; 
   }else{ 
      $doesntExist = "Invalid user."; 
      include 'signin.inc'; 
      die(); 
   } 

   // Check password 
   $password = $_POST['password']; 
   if(isset($password)){ 

   $query = "SELECT * FROM users WHERE username = '$username' &&  password = '$password'"; 
   $result = mysql_query($query); 

   if(mysql_num_rows($result) == 1){ 
      $correct_password = "TRUE"; 
   }else{ 
      $wrong_pass = "Incorrect password."; 
      include 'signin.inc'; 
   } 

}else{ 
   $notEntered = "No username was entered."; 
   include 'signin.inc'; 
} 

if($user_exists == "TRUE" && $correct_password == "TRUE"){ 
    echo "User name and password correct."; 
} 

}

?>
Image Image
Caped Knight
Forum Commoner
Posts: 33
Joined: Wed Jan 01, 2003 6:20 pm
Location: Somewhere...out there...

Post by Caped Knight »

Nope, I did end my first if statement, and I still get the error as well.
User avatar
Sevengraff
Forum Contributor
Posts: 232
Joined: Thu Apr 25, 2002 9:34 pm
Location: California USA
Contact:

Post by Sevengraff »

Code: Select all

// Check password 
   $password = $_POST['password']; 
   if(isset($password)){
need to close that if statement i think.
Caped Knight
Forum Commoner
Posts: 33
Joined: Wed Jan 01, 2003 6:20 pm
Location: Somewhere...out there...

Post by Caped Knight »

Ahh, never mind that post. I was having trouble saving the file. Thanks again, everyone.
Post Reply