5 Replies - 306 Views - Last Post: 21 October 2017 - 12:32 PM Rate Topic: -----

#1 sadaiyappan_  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 04-October 17

Why do this code always return true for the $result?

Posted 20 October 2017 - 11:47 PM

See:

<!DOCTYPE HTML>  
<html>
<head>
<style>
</style>
</head>
<body>

<?php
	$name = $password = "";
?> 

	<p>
<form method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
name: <input type="text" name="name"><br><br>
password: <input type="text" name="password"><br><br>
<input type="submit" name="login" value="Submit">  
</form>
	</p>

<?php
	
define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'root');
define('DB_PASSWORD', '');
define('DB_NAME', 'test');

$conn = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME);

$sql = "SELECT name FROM stuff WHERE name = '$name' AND password = '$password'";

$result = mysqli_query($conn, $sql);

if(isset($_POST['login'])){
    if($result == TRUE)
	{
		echo "Successful login!";
	}	
		else{
		echo "Login failed!";
	}
    }
?>	

</body>
</html>










Is This A Good Question/Topic? 0
  • +

Replies To: Why do this code always return true for the $result?

#2 andrewsw  Icon User is offline

  • the case is sol-ved
  • member icon

Reputation: 6379
  • View blog
  • Posts: 25,770
  • Joined: 12-December 12

Re: Why do this code always return true for the $result?

Posted 21 October 2017 - 01:25 AM

mysqli_query: the docs

see 'Return Values'
It isn't returning TRUE, but a result object.

N.B. You shouldn't query the database if the name and password haven't been supplied, it is a wasted trip.
Was This Post Helpful? 0
  • +
  • -

#3 sadaiyappan_  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 04-October 17

Re: Why do this code always return true for the $result?

Posted 21 October 2017 - 01:26 AM

Yes but according to docs it should return false. It never returns false for some reason?
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is offline

  • the case is sol-ved
  • member icon

Reputation: 6379
  • View blog
  • Posts: 25,770
  • Joined: 12-December 12

Re: Why do this code always return true for the $result?

Posted 21 October 2017 - 01:34 AM

It returns FALSE on failure. Failure is not the same as no matches.
Was This Post Helpful? 0
  • +
  • -

#5 sadaiyappan_  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 04-October 17

Re: Why do this code always return true for the $result?

Posted 21 October 2017 - 02:31 AM

Ahh.
Was This Post Helpful? 0
  • +
  • -

#6 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3714
  • View blog
  • Posts: 13,469
  • Joined: 08-August 08

Re: Why do this code always return true for the $result?

Posted 21 October 2017 - 12:32 PM

Deprecated MySQL functions are simpler to use (not counting all the sanitizing you need to add) than PDO or MySQLi prepared statements, but far less secure. That's why they've been deprecated. If you're just learning PHP you should start with
http://www.dreaminco...duction-to-pdo/
or
http://php.net/manua...-statements.php
When dealing with old code that uses deprecated MySQL functions you should replace them if possible. If it's not possible, your experience with PDO or MySQLi will benefit you when dealing with MySQL functions.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1