How to check if user already exists in MySQL with PHP

前端 未结 4 1059
失恋的感觉
失恋的感觉 2020-12-10 00:31

I am using following code which is not working for me.

$con=mysqli_connect(\"localhost\",\"root\",\"\",\"my_db\");
$check=\"SELECT COUNT(*) FROM persons WHER         


        
4条回答
  •  天命终不由人
    2020-12-10 01:03

    mysqli_query function returns a resultset handle. You then need to read the rows from it:

    $rs = mysqli_query($con,$check);
    $data = mysqli_fetch_array($rs, MYSQLI_NUM);
    if($data[0] > 1) {
        //user exists;
    }
    

    Also note that SELECT count(1) FROM ... will be faster than SELECT count(*) FROM ... You won't see much of a difference in a small table, but with a large table of several hundred thousand rows, the difference may be significant.

提交回复
热议问题