Home > Article > Backend Development > How to determine if username does not exist in php
How does php determine that the username does not exist
First get the username through "$_POST"; then use PDO to query the username Obtain the user information of the user name; finally obtain the number of returned data through the "count()" function to determine if it is less than 1, the user name does not exist.
$username = $_POST['username']; try{ //连接数据库 $pdo=new PDO('dsn', 'username', 'password'); } catch(\PDOException $e) { exit("连接错误:$e->getMessage()"); } //查询 $sql = "select * from user where username=? limit 1"; //准备sql模板 $stmt = $pdo->prepare($sql); //绑定参数 $stmt->bindValue(1, $usenname); //执行预处理语句 $stmt->execute(); //推荐这种方式来获取查询结果 $data = $stmt->fetch(PDO::FETCH_ASSOC); if (count($data) < 1) { echo "用户名不存在!"; } //释放查询结果 $stmt = null; //关闭连接 $pdo = null;
The above is the detailed content of How to determine if username does not exist in php. For more information, please follow other related articles on the PHP Chinese website!