Warning: mysql_fetch_assoc() expects parameter 1 to be resource?

这个报错是不是数据库的问题?求大佬解答下,谢谢!

<?php
$con = mysqli_connect( 
 'localhost', /* The host to connect to 连接MySQL地址 */   
 'root',   /* The user to connect as 连接MySQL用户名 */   
 'root', /* The password to use 连接MySQL密码 */   
 'wechat');  /* The default database to query 连接数据库名称*/  
 
$sql = "SELECT * FROM abc";

$result = mysqli_query($con, $sql);

//将数据给弄出来
if (mysqli_num_rows($result) > 0){
    
$data = array();
while ($row = mysqli_fetch_assoc($result)) {
     $data[] = $row;
}

//随机个数,默认1
$rand_times = 1;
$rand_times = count($data)<$rand_times?count($data):$rand_times;
$ress = array();
for( $i=0;$i<$rand_times;$i++ ){
    $ress[] = $data[rand(0,count($data)-1)][0]; 
    echo $ress;
}
}
?>

clipboard.png

clipboard.png

阅读 4.2k
3 个回答
新手上路,请多包涵

已解决
clipboard.png

php mysql_fetch_assoc使用错误 看PHP手册

mysql_fetch_assoc()的参数必须是 对 mysql_query() 的调用返回的结果集。

你打印一下$result看看是什么吧,补充一下信息

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题