Table of Contents
回复讨论(解决方案)
Home Backend Development PHP Tutorial php数据库问题,求助。。。。

php数据库问题,求助。。。。

Jun 23, 2016 pm 02:19 PM

初学PHP来的,想自己写一个登陆的页面,但现在连验证帐号都过不去,请大家看下我的代码是哪里有问题 

<?php $user123 = $_POST['user']; $db = new mysqli($sqln,$sqlu,$sqlp,$sqld) or die("Error in the consult.." . mysqli_error($db)); $query = "SELECT 'user_email' FROM 'user' WHERE 'user_email' = '$user123'"; // mysql_select_db($sqld); mysqli_select_db($db, $sqld); $result = $db->query($query) or die("Error in the consult.." . mysqli_error($db)); if ($result == 1){ echo "<p> 登陆成功 </p>"; } elseif ($result == 0){ echo "<p> 没有找到 </p>"; } echo $_POST["user"]; $dba = $db->affected_rows; echo $dba; mysqli_close($db); ?> 
Copy after login

$result = $db->query($query) or die("Error in the consult.." . mysqli_error($db));
这句不加后面的错误显示的话后面的 echo $dba 显示的是-1,但是我输入的帐号在数据库里面是有的。
我输入的user123是 vvvvy
但是网页打开来提交后显示这个错误 Error in the consult..You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''user' WHERE 'user_email' = 'vvvvy'' at line 1


回复讨论(解决方案)

复制以下语句应该就可以了

$query = "SELECT user_email FROM `user` WHERE user_email = '$user123'";
Copy after login

你的程序两个地方错误
第一:你的sql语句里的符号用错了,字段和表是不是可以用单引号的引起来的
可以改成:$query = "SELECT user_email FROM `user` WHERE user_email = '$user123'";
第二:因为你的$result的值要么是false要么就是ID,所你的判断条件有问题,你可以直接不用等于1或0
例如:if($result){
...
}
else if($result){
...
}

你的程序两个地方错误
第一:你的sql语句里的符号用错了,字段和表是不是可以用单引号的引起来的
可以改成:$query = "SELECT user_email FROM `user` WHERE user_email = '$user123'";
第二:因为你的$result的值要么是false要么就是ID,所你的判断条件有问题,你可以直接不用等于1或0
例如:if($result){
...
}
else if($result){
...
}

我全部按照你的修改了,但是不管输入对错还是显示不存在。。。

我GOOGLE了下,发现现在语句啥的也没有什么问题,难道数据库设置有问题?我的是本地的,用的是wamp~

$query = "SELECT 'user_email' FROM 'user' WHERE 'user_email' = '$user123'";

$query = "SELECT `user_email` FROM `user` WHERE `user_email` = '$user123'";
是一样的吗?

$query = "SELECT 'user_email' FROM 'user' WHERE 'user_email' = '$user123'";

$query = "SELECT `user_email` FROM `user` WHERE `user_email` = '$user123'";
是一样的吗?
$query = "SELECT `user_email` FROM `user` WHERE `user_email` = '$user123'";
我把这句改成了。
$query = "SELECT user_email FROM `user` WHERE user_email = '$user123'";


$query = "SELECT 'user_email' FROM 'user' WHERE 'user_email' = '$user123'";

$query = "SELECT `user_email` FROM `user` WHERE `user_email` = '$user123'";
是一样的吗?
$query = "SELECT `user_email` FROM `user` WHERE `user_email` = '$user123'";
我把这句改成了。
$query = "SELECT user_email FROM `user` WHERE user_email = '$user123'";


..为什么不能编辑帖子。。
我改了这句后无论输入什么都是对的?

mysqli::query -- mysqli_query ? 对数据库执行一次查询

返回值
失败时返回 FALSE,通过 mysqli_query() 成功执行SELECT, SHOW, DESCRIBE或 EXPLAIN查询会返回一个mysqli_result 对象,其他查询则返回TRUE。


因此,即使你输入库中没有的记录,只要sql没有错误,也是返回mysqli_result 对象。而 "结果集==1" 永远为真,所以就出现你说的输入什么都是对的了。

正确的做法是用它查询出来的行数与1比较就对了。

if ($result->num_rows == 1){     echo "<p> 登陆成功 </p>"; } else {     echo "<p> 没有找到 </p>"; }
Copy after login
Copy after login


你的程序两个地方错误
第一:你的sql语句里的符号用错了,字段和表是不是可以用单引号的引起来的
可以改成:$query = "SELECT user_email FROM `user` WHERE user_email = '$user123'";
第二:因为你的$result的值要么是false要么就是ID,所你的判断条件有问题,你可以直接不用等于1或0
例如:if($result){
...
}
else if($result){
...
}

我全部按照你的修改了,但是不管输入对错还是显示不存在。。。

我GOOGLE了下,发现现在语句啥的也没有什么问题,难道数据库设置有问题?我的是本地的,用的是wamp~
我的这个这样写也有点不对,应该直接else {...}就可以了,不要用else if($result){...}

梳理下你的if判断,还有执行的sql是否有误。

mysqli::query -- mysqli_query ? 对数据库执行一次查询

返回值
失败时返回 FALSE,通过 mysqli_query() 成功执行SELECT, SHOW, DESCRIBE或 EXPLAIN查询会返回一个mysqli_result 对象,其他查询则返回TRUE。


因此,即使你输入库中没有的记录,只要sql没有错误,也是返回mysqli_result 对象。而 "结果集==1" 永远为真,所以就出现你说的输入什么都是对的了。

正确的做法是用它查询出来的行数与1比较就对了。

if ($result->num_rows == 1){     echo "<p> 登陆成功 </p>"; } else {     echo "<p> 没有找到 </p>"; }
Copy after login
Copy after login
 

一直想获取我查询的数据,但没有找到函数,英语又不会。。。谢谢哥们了。。。如果用 $result->num_rows 来检测帐号密码会不会不安全?

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Explain the concept of late static binding in PHP. Explain the concept of late static binding in PHP. Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

Framework Security Features: Protecting against vulnerabilities. Framework Security Features: Protecting against vulnerabilities. Mar 28, 2025 pm 05:11 PM

Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.

Customizing/Extending Frameworks: How to add custom functionality. Customizing/Extending Frameworks: How to add custom functionality. Mar 28, 2025 pm 05:12 PM

The article discusses adding custom functionality to frameworks, focusing on understanding architecture, identifying extension points, and best practices for integration and debugging.

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

What exactly is the non-blocking feature of ReactPHP? How to handle its blocking I/O operations? What exactly is the non-blocking feature of ReactPHP? How to handle its blocking I/O operations? Apr 01, 2025 pm 03:09 PM

An official introduction to the non-blocking feature of ReactPHP in-depth interpretation of ReactPHP's non-blocking feature has aroused many developers' questions: "ReactPHPisnon-blockingbydefault...

See all articles