当前位置:Gxlcms > PHP教程 > 关于mysql_fetch_row的问题

关于mysql_fetch_row的问题

时间:2021-07-01 10:21:17 帮助过:17人阅读

请问各位高手能用if来判断mysql_fetch_row吗,我看到一个php视频中老师写的
if($row=mysql_fetch_row($res)),没有写错,就是一个=,视频中程序也正常运行了,
可是我按着写浏览器却总是提示这行语法有错误,请高手指点


回复讨论(解决方案)

while($row=mysql_fetch_row($res)){

}

LZ你浏览器提示的是怎么一个错误呢。
mysql_fetch_row()这条语句中的参数(也就是$res)必须是MYSQL返回的资源类型才行。
普通的自定义数组、PHP变量应该是不行的。
if($row = mysql_fetch_row($res))这句话是可以用的。

那种写法是可以的
建议你把错误信息贴出来以供分析

Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource
这是错误信息

那人家都告诉你了 说你用的参数不是一个有效的MYSQL返回资源类型
比如 你在数据库读取的时候
$a = mysql_query();
mysql_fetch_row($a);
这样才好用吧

这是写的代码

$id=$_post['id'];

$password=$_post['password'];

$conn=mysql_connect("localhost","root","1234")or die("连接失败".mysql_errno());

mysql_select_db("test",$conn)or die("选择数据库失败".mysql_errno());

mysql_query('set names utf8') or die("设置编码失败".mysql_errno());

$sql="select password from admin where id=$id";

$res=mysql_query($sql,$conn);

if ($row=mysql_fetch_row($res)){

if ($row[1]==$password){

header('location:empMain.php');
exit();
}
}
header('location:login.php');
exit();

mysql_free_result($res);
mysql_close();
?>

resource(4) of type (mysql result)
$res=mysql_query($sql,$conn);
//LZ在这里var_dump($res),只要是显示resource(4) of type (mysql result)就对了
//如果不是那就是之前数据库连接或者内容上的问题了吧
if ($row=mysql_fetch_row($res)){


引发错误的不是数据库操作,而是
$id=$_post['id'];
$password=$_post['password'];
应为
$id=$_POST['id'];
$password=$_POST['password'];
注意,php的变量时大小写敏感的

由于并不存在 $_post 数组,而你又屏蔽了 NOTICE 级别的错误(不然早就发现了)
于是
$sql="select password from admin where id=$id";
变成了 $sql="select password from admin where id=";
显然是错误的!
因此导致
Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource
的出现

楼主威武,语气极其自信,肯定,实力超凡啊

果断是参数类型的问题,mysql_fetch_row($res)参数必须是资源类型的

人气教程排行