-1

How to compare Mysql data exist in php

<?php

if (isset($_POST['btnReg'])) {
    $user2 = mysql_real_escape_string($_POST['reg_user']);
    $query = "SELECT * FROM `accounts` WHERE `Username` = '" . $user2 . "' LIMIT 1";
    $result = DataProvider::execNonQueryAffectedRows($query);
    if ($result == 1) {
        echo '<div class="alert alert-danger"><strong>Username already exists</strong></div>';
    } else {
        echo '<div class="alert alert-success"><strong>Sign Up Success .</strong></div>';
    }
}
?>

I'm try , but when i use other username it show Username Already Exist . Check in code $result . Pls help me , Thanks you

Ali Zia
  • 3,825
  • 5
  • 29
  • 77
  • what's your target dude? – Webster Jan 04 '16 at 06:40
  • Register new accounts , but i check account does not exists it show error ( Username Already Exist ) you can check in my Website . http://sampsv.net/dangnhap.php – Bieber Kieu Jan 04 '16 at 06:43
  • `SELECT` query doesn't `affect` rows. Check for returned-set rows count instead, or modify query to `SELECT count(id) as count FROM "accounts" WHERE "Username" = '".$user2."' LIMIT 1` and check for something like `$row = DataProvider::execQueryGetFirstRow($query); if (intval(@$row['count'])) { ... }` – ankhzet Jan 04 '16 at 06:46
  • please check your query result once. what results you get from query? –  Jan 04 '16 at 07:01
  • The only way this feature can work is that you add `UNIQUE` constraint to your `Username` column. **You don't** check if user exists. You just insert. If the `Username` exists, database will tell you and PHP will interpret that as an `Exception`. You can't check for username first by querying. You will receive false results and you will end up with duplicated data. – Mjh Jan 04 '16 at 09:07
  • Please [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). [These extensions](http://php.net/manual/en/migration70.removed-exts-sapis.php) have been removed in PHP 7. Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really pretty easy](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Jan 04 '16 at 13:08

2 Answers2

0

change this

$result = DataProvider::execNonQueryAffectedRows($query);

to this

$num_rows = mysql_num_rows(mysql_query($result));

then use this to replace your previous if statement

if($num_rows == 1){
// your stuff
}else{
// your stuff
}
Webster
  • 1,113
  • 2
  • 19
  • 39
0

Try this way:-

    $servername = "localhost";
    $username = "your_username";
    $password = "your_password";
    $dbname = "your_db";

    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    } 


    if (isset($_POST['btnReg'])) {

      $user2=mysqli_real_escape_string($conn, $_POST["reg_user"]);
     // mysqli_real_escape_string is used instead of mysql_real_escape_string

         $sql =  "SELECT * FROM accounts WHERE Username = '$user2' LIMIT 1";
         $result = $conn->query($sql);

        if ($result->num_rows > 0) {
           echo '<div class="alert alert-danger"><strong>Username already exists</strong></div>';
           } 
           else {
           echo '<div class="alert alert-success"><strong>Sign Up Success .</strong></div>';
           }
    }
    $conn->close();
Ravi Hirani
  • 6,511
  • 1
  • 27
  • 42