如何比较php

时间:2016-01-04 06:38:20

标签: php mysql

如何比较php中存在的Mysql数据

<?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>';
    }
}
?>

我正在尝试,但是当我使用其他用户名时,它会显示用户名已存在。签入代码$ result。请帮助我,谢谢你

2 个答案:

答案 0 :(得分:0)

更改此

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

到这个

if($num_rows == 1){
// your stuff
}else{
// your stuff
}

然后使用它来替换之前的if语句

SELECT  * 
FROM    EMPLOYEE E
JOIN    PUNCHES P
ON      E.EMPID =  P.EMPID
WHERE   E.ACTIVE = 'Y'
AND     (P.PUNCH_TIME IS NULL OR P.DELETED = 'Y')

答案 1 :(得分:0)

尝试这种方式: -

    $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();