-2

I've just started doing a bit of PHP coding and was wondering how to do a function that returns TRUE or FALSE when the username and password are checked against the database. If they are there I want to return as TRUE and if they aren't... FALSE. My tables called 'P_USER'.

So far I have this:

<?php

  echo "<pre>";
  print_r($_POST);

  $username = $_POST["username"];
  //echo $username;

  $password = $_POST["password"];
  //echo $username;

  $query = mysql_query("SELECT * FROM P_USER");
Joel Hinz
  • 24,719
  • 6
  • 62
  • 75
TheGarrett
  • 271
  • 2
  • 23
  • 1
    http://stackoverflow.com/questions/19891355/php-password-verify-issue – Mark Baker Feb 16 '15 at 10:23
  • Stop using the `mysql` PHP extension (function names starting with `mysql_`). It is [deprecated and will be removed](http://php.net/manual/en/function.mysql-query.php) in the near future. Consider learning the [`mysqli`](http://php.net/manual/en/book.mysqli.php) or [`PDO`](http://php.net/manual/en/book.pdo.php) extensions instead. – axiac Feb 16 '15 at 10:34

1 Answers1

1

Database cannot return true or false as you want. But there is a work around.

You can check the value of username and password and if it matches then the query will return 1 else it will return 0.

The query is:

SELECT count(*) FROM P_USER WHERE username = $username and password = $password;

Provided that the username is unique in you table.

Then you can check the result of the query and then perform the action as desired.

Wolfack
  • 2,667
  • 1
  • 26
  • 50