php数据库问题,求助。。。。
初学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); ?>
$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'";
你的程序两个地方错误
第一:你的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>"; }
你的程序两个地方错误
第一:你的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>"; }
一直想获取我查询的数据,但没有找到函数,英语又不会。。。谢谢哥们了。。。如果用 $result->num_rows 来检测帐号密码会不会不安全?

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Alipay PHP...

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,

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

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

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

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�...

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.

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...
