Home > Backend Development > PHP Tutorial > PHP执行SQL后判断记录集不为空的语句

PHP执行SQL后判断记录集不为空的语句

WBOY
Release: 2016-06-06 20:15:20
Original
1052 people have browsed it

<code><?php $sql = "select * from mytable where name='$user_name'";
$result =mysql_query($sql,$con);
    if($result && mysql_num_rows($result)>0){
    echo '<hr>记录集不为空!!!!!,说明已经有这个用户名了';
    //执行更新操作
    }else {
    //新增数据
    }
?></code>
Copy after login
Copy after login

为什么明明有这个用户却不echo呢?

回复内容:

<code><?php $sql = "select * from mytable where name='$user_name'";
$result =mysql_query($sql,$con);
    if($result && mysql_num_rows($result)>0){
    echo '<hr>记录集不为空!!!!!,说明已经有这个用户名了';
    //执行更新操作
    }else {
    //新增数据
    }
?></code>
Copy after login
Copy after login

为什么明明有这个用户却不echo呢?

把$result去掉,它是一个资源类型resource,资源类型是无法进行布尔判断的

解决了吗?没解决的话你先打印一下$con,或者先把$result =mysql_query($sql,$con)里面的$con去掉试试,会默认打开上次连接,如果去掉可以,或者打印出来为空,说明你的连接对象没连接成功,检查一下初始化的时候给连接对象赋值成功了吗?没成功的原因,就是用户密码以及连接地址有问题,我自己是忘记$this->conn,直接mysql_connect了,然后后面调用$this->conn的就是null了。哈哈

也可以 !empty($result)

Related labels:
php
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