今天测试数据库连接,遍历数据表的中信息的是一直出现以下的错误:
Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource in D:\AppServ\www\wordpress\lianjie001.php on line 20
我原来写的代码是这样的:
<?php
$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 "
echo "
echo "
echo "
echo "";
}
mysql_close($connection);
?>
检查半天也没有发现错误,后来经同事指点是缺少了选择数据库的代码如下所示:
$db_selecct=mysql_select_db($db_database);
完整的代码就是下面这样即可完成遍历。
<?php
$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 "
echo "
echo "
echo "
echo "";
}
mysql_close($connection);
?>
回复讨论(解决方案)
这到底是来提问的呢?还是来分享经验的呢?
结贴率为0,估计是一时心血来潮过来玩玩的吧
授人以鱼不如授人以渔
echo mysql_error();
mysql_query($sql) or die(mysql_error());
4楼正解.........