Home > Backend Development > PHP Tutorial > 关于mysql_query返回值的问题

关于mysql_query返回值的问题

WBOY
Release: 2016-06-23 13:28:24
Original
865 people have browsed it

错误代码

<?php/** * Created by PhpStorm. * User: Administrator * Date: 2015/8/16 * Time: 15:37 */if(!isset($_POST['name'])){    die('user name not define');}if(!isset($_POST['password'])){    die('user name not define');}if(empty($_POST['name'])){    die('user name is empty');}if(empty($_POST['password'])){    die('user password is empty');}require_once 'connect_server.php';connectdb();$name=$_POST['name'];$password=$_POST['password'];$sql="select * from users WHERE name=$name AND password=$password";$result=mysql_query($sql);$new=mysql_fetch_assoc($result);if($new['name']==$name&&$new['password']==$password){    echo "<script>alert('登陆成功');window.location='zhuye.php'</script>";}else{    echo "<script>alert('登陆失败');window.location='zhuye.html'</script>";}
Copy after login

可运行代码
<?phprequire_once 'function.php'?><!DOCTYPE html><html><head>    <meta charset="UTF-8">    <title>所有用户</title></head><body><a href="adduser.html">添加用户</a><?php$conn=connectDb();mysql_select_db('test',$conn);$result=mysql_query("SELECT * FROM users",$conn);$dataCount=mysql_num_rows($result);echo"<table border='1'>";for($i=1;$i<$dataCount;$i++){    $result_arr=mysql_fetch_assoc($result);    $id=$result_arr['id'];    $name=$result_arr['name'];    $age=$result_arr['age'];    echo"<tr><td>$id</td><td>$name</td><td>$age</td><td><a href='edituser.php?id=$id>'>修改</a><br> <a href='deleteuser.php?id=$id>'>删除</a> </td></tr>";}echo"</table>";?></body></html>
Copy after login

两段代码中的mysql_query的返回值一样,但是为什么一段函数在调用其返回值的时候出现警告如图


回复讨论(解决方案)

"select * from users WHERE name=$name AND password=$password"
Copy after login

上面的代码是有问题的,name和password没有加引号,导致sql语法错误,最终没有返回资源类型,而返回了布尔值false
正确的代码应该是
"select * from users WHERE 'name'=$name AND 'password'=$password"
Copy after login

$sql="select * from users WHERE name=$name AND password=$password";

改为:

$sql="select * from users WHERE name='".$name."' AND password='".$password."'";

因为你$name,$password如果为空,会造成语法错误。

还是错的?把sql语句再改成这样试试,应该是$name和$password没有被当成字符串导致出错。。。

"select * from users WHERE 'name'=".$name." AND 'password'=".$password
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

还是错的?把sql语句再改成这样试试,应该是$name和$password没有被当成字符串导致出错。。。

"select * from users WHERE 'name'=".$name." AND 'password'=".$password
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login



我又写错了。。。应该是这样
"select * from users WHERE name='".$name."' AND password='".$password."'";
Copy after login
Copy after login
Copy after login
Copy after login


还是错的?把sql语句再改成这样试试,应该是$name和$password没有被当成字符串导致出错。。。

"select * from users WHERE 'name'=".$name." AND 'password'=".$password
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login



我又写错了。。。应该是这样
"select * from users WHERE name='".$name."' AND password='".$password."'";
Copy after login
Copy after login
Copy after login
Copy after login

可否告知为什么

$sql="select * from users WHERE name=$name AND password=$password";

改为:

$sql="select * from users WHERE name='".$name."' AND password='".$password."'";

因为你$name,$password如果为空,会造成语法错误。

可以告知为什么吗?



还是错的?把sql语句再改成这样试试,应该是$name和$password没有被当成字符串导致出错。。。

"select * from users WHERE 'name'=".$name." AND 'password'=".$password
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login



我又写错了。。。应该是这样
"select * from users WHERE name='".$name."' AND password='".$password."'";
Copy after login
Copy after login
Copy after login
Copy after login

可否告知为什么



因为$name和$password在sql语句中应该是以字符串的形式出现的(也就是说得有单引号包起来$name和$password的值)

还是说,你想知道为什么要那么拼接单引号和双引号?




还是错的?把sql语句再改成这样试试,应该是$name和$password没有被当成字符串导致出错。。。

"select * from users WHERE 'name'=".$name." AND 'password'=".$password
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login



我又写错了。。。应该是这样
"select * from users WHERE name='".$name."' AND password='".$password."'";
Copy after login
Copy after login
Copy after login
Copy after login

可否告知为什么



因为$name和$password在sql语句中应该是以字符串的形式出现的(也就是说得有单引号包起来$name和$password的值)

还是说,你想知道为什么要那么拼接单引号和双引号?

我想知道为什么拼接单引号和双引号还有为什么要用将变量放在..的中间,两个点的作用是什么

$sql="select * from users WHERE name=$name AND password=$password";
如果是数值型的$name和$password变量,可以在SQL语句中直接使用,如果是字符串类型的变量,需要加单引号

我把代码拆成三段,你应该能明白了。两个那个点是用来连接字符串的,意思是告诉PHP:点之后的变量是个字符串

"select * from users WHERE name='"
Copy after login
Copy after login

"' AND password='"
Copy after login
Copy after login

"'"
Copy after login
Copy after login

就是说,每个单引号 / 双引号碰到第二个单引号 / 双引号时,那一段字符就结束了。这三段都是一个双引号里包单引号。。

我把代码拆成三段,你应该能明白了。两个那个点是用来连接字符串的,意思是告诉PHP:点之后的变量是个字符串

"select * from users WHERE name='"
Copy after login
Copy after login

"' AND password='"
Copy after login
Copy after login

"'"
Copy after login
Copy after login

就是说,每个单引号 / 双引号碰到第二个单引号 / 双引号时,那一段字符就结束了。这三段都是一个双引号里包单引号。。

懂了~谢谢
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template