0

i am trying to check a data existence from mysql table but following script not working. bellow my codes are provided please find out where is my mistake there.

<?php
//including the database files
include("../inc/settings.php");

$email = $_POST['email'];
$password = $_POST['password'];

$query = mysql_query("SELECT easy123 FROM users WHERE email=$email", $conn);

  if (mysql_num_rows($query) != 0)
  {
      echo "Username already exists";
  }

  else
  {
    echo "this username not used";
  }

?>

The error i am getting is-

Warning: mysql_query() expects parameter 2 to be resource, object given in C:\xampp\htdocs\myfiles\Easy123\master\login.php on line 8

Warning: mysql_num_rows() expects parameter 1 to be resource, null given in C:\xampp\htdocs\myfiles\Easy123\master\login.php on line 10 this username not used

amdixon
  • 3,814
  • 8
  • 25
  • 34
Liakat Hossain
  • 1,288
  • 1
  • 13
  • 24
  • Check your database connection in `settings.php` file . `$conn` is undefine in your code – Saty Nov 28 '15 at 11:57
  • 1
    An email address would typically be a string, and string literals need to be quoted in SQL – Mark Baker Nov 28 '15 at 11:57
  • 2
    Although you should move into the 21st century, and start using prepared statements/bind variables with MySQLi or PDO, especially as the newest version of PHP is dropping the MySQL extension completely – Mark Baker Nov 28 '15 at 11:58

2 Answers2

1

First of all, make sure your database connection is correctly set up. The error you're getting clearly says that your $conn variable isn't a valid resource.

Also, use prepared statements and parameterized queries. Do not use PHP variables within your query string, it's not secure at all. Use instead PDO or MySQLi

  1. Using PDO:

    $stmt = $pdo->prepare('SELECT easy123 FROM users WHERE email = :email');
    
    $stmt->execute(array('email' => $email));
    
    foreach ($stmt as $row) {
        // do something with $row
    }
    
  2. Using MySQLi:

    $stmt = $dbConnection->prepare('SELECT easy123 FROM users WHERE email = ?');
    $stmt->bind_param('s', $email);
    
    $stmt->execute();
    
    $result = $stmt->get_result();
    while ($row = $result->fetch_assoc()) {
        // do something with $row
    }
    
ulentini
  • 2,413
  • 1
  • 14
  • 26
0

Your $query seems to be wrong. Try this:

$query = mysql_query("SELECT easy123 FROM users WHERE email='$email'", $conn);

Make sure $conn is properly defined aswell.

Jesse
  • 76
  • 1
  • 9