How to determine if username does not exist in php

Guanhui
Release: 2023-02-28 22:44:01
Original
3052 people have browsed it

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;
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!