Home  >  Article  >  Backend Development  >  How to determine if username does not exist in php

How to determine if username does not exist in php

Guanhui
GuanhuiOriginal
2020-05-07 16:55:423124browse

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn