当前位置:Gxlcms >
PHP教程 >
php错误提示:mysql_fetch_row():suppliedargumentisnotavalidMySQL
php错误提示:mysql_fetch_row():suppliedargumentisnotavalidMySQL
时间:2021-07-01 10:21:17
帮助过:17人阅读
今天测试数据库连接,遍历数据表的中信息的是一直出现以下的错误:
Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource in D:\AppServ\www\wordpress\lianjie001.php on line 20
我原来写的代码是这样的:
$db_host="localhost";
$db_database="wordpress";
$db_username="root";
$db_password="admin123";
$connection=mysql_connect($db_host,$db_username,$db_password);
mysql_query("set names 'utf8'");
$sql="select * from huanzhexinxi";
$result=mysql_query($sql);
while($resultrowzhen=mysql_fetch_row(($result))){
$huanname=$resultrowzhen[0];
$huanage=$resultrowzhen[1];
$huancall=$resultrowzhen[2];
$huanliu=$resultrowzhen[3];
echo "
";
echo "名字:$huanname | ";
echo "年龄:$huanage | ";
echo "电话:$huancall | ";
echo "患者留言:$huanliu | ";
echo "";
}
mysql_close($connection);
?>
检查半天也没有发现错误,后来经同事指点是缺少了选择数据库的代码如下所示:
$db_selecct=mysql_select_db($db_database);
完整的代码就是下面这样即可完成遍历。
$db_host="localhost";
$db_database="wordpress";
$db_username="root";
$db_password="admin123";
$connection=mysql_connect($db_host,$db_username,$db_password);
mysql_query("set names 'utf8'");
$db_selecct=mysql_select_db($db_database);
$sql="select * from huanzhexinxi";
$result=mysql_query($sql);
while($resultrowzhen=mysql_fetch_row(($result))){
$huanname=$resultrowzhen[0];
$huanage=$resultrowzhen[1];
$huancall=$resultrowzhen[2];
$huanliu=$resultrowzhen[3];
echo "
";
echo "名字:$huanname | ";
echo "年龄:$huanage | ";
echo "电话:$huancall | ";
echo "患者留言:$huanliu | ";
echo "";
}
mysql_close($connection);
?>
回复讨论(解决方案)
这到底是来提问的呢?还是来分享经验的呢?
结贴率为0,估计是一时心血来潮过来玩玩的吧
授人以鱼不如授人以渔
echo mysql_error();
mysql_query($sql) or die(mysql_error());
4楼正解.........
人气教程排行