Home  >  Article  >  Backend Development  >  PDO查询不成功!

PDO查询不成功!

WBOY
WBOYOriginal
2016-06-23 13:49:10888browse

代码如下:

<html><meta type="text/html" charset=utf8><form action="form.php" method="POST"><p>名字: <input type="text" name="name" /></p><p>密码: <input type="password" name="password" /></p><input type="submit" /></form><?php error_reporting(0); $dsn = "mysql:host=localhost;dbname=liuyan"; $db = new PDO($dsn, 'root', 'root'); $name = $_POST['name']; $password = md5($_POST['password']); //$date = date("Y-m-d H:i:s"); $sql = "select * from admin where user_name=? and password=?"; $stm = $db->prepare($sql); $stm->execute(array($name,$password)); $rs=$stm->fetch(); var_dump($rs); if(empty($rs)){    echo "<script>alert('查询失败');</script>";} else{    echo "<script>alert('查询成功');</script>";} ?></html>


admin表


现在问题是:fetch取出来用empty判断返回false,为啥会这样?


回复讨论(解决方案)

大神快来啊。。。

$sql = "select * from admin where user_name=? and password=?";
这句查询什么?

注释掉 error_reporting(0);
在 16、17 行后面加入 print_r($db->errorinfo());

$sql = "select * from admin where user_name=? and password=?";
这句查询什么?

查询admin表中是否有该用户!通过用户名和md5加密后的密码

注释掉 error_reporting(0);
在 16、17 行后面加入 print_r($db->errorinfo());

加了大神,输出:Array ( [0] => 00000 [1] => [2] => )  说明什么问题?

说明没有出现错误
因此可以认为你的表中没有符合条件的记录

说明没有出现错误
因此可以认为你的表中没有符合条件的记录

字段长度定少了,md5加密后的密码长度是32位,我只给了30位,所以密码少了2位!谢谢各位!
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