user3268379
user3268379

Reputation: 141

Find username within specific column

I am trying to get this PHP working where it returns true or false if the username exists in the username column on the database:

$data = array($_POST["username"]);


$db = new PDO('mysql:host=localhost;dbname=Example;charset=utf8', 'Example', 'Example');

$stmt = $db->prepare("SELECT FROM Members (username) WHERE username=?");
$stmt->execute($data);

 if(mysql_num_rows($stmt)>=1)
   {
    echo"true";
   }
 else
    {
    echo "false";
    }

Upvotes: 0

Views: 39

Answers (4)

Ravinder Reddy
Ravinder Reddy

Reputation: 23992

You can fetch a boolean result, from the sql query itself, with minor modification.

SELECT
  count(username) > 0 as user_exists
FROM Members
WHERE username=?

In PHP script just read the result that has either a true (1) or false (0) as value.

Upvotes: 0

Nagaraj S
Nagaraj S

Reputation: 13474

Try this statement

SELECT * FROM Members WHERE username

or

SELECT username FROM Members WHERE username

Upvotes: 0

Danil Speransky
Danil Speransky

Reputation: 30453

SELECT * FROM Members WHERE username=?

Upvotes: 0

Abhik Chakraborty
Abhik Chakraborty

Reputation: 44844

SELECT FROM

Does not select anything.

You need to specify what you need to select or just * to select all

SELECT `username` FROM 

OR

SELECT * FROM

Upvotes: 1

Related Questions