警告:mysql\u fetch\u array()期望参数1是resource…-为什么?

egdjgwm8  于 2021-06-18  发布在  Mysql
关注(0)|答案(1)|浏览(344)

考虑以下代码:

<?php

$db_error='Establishing a Database Connection Error';
$localhost='localhost';
$db_user='root';
$db_name='uploader_db';
$db_pass='';
$db_connect=mysql_connect($localhost,$db_user,$db_pass) or die($db_error);
  mysql_select_db($db_name) or die($db_error);
 $result="SELECT 'user','pass','name','email' FROM 'config' ORDER BY 'name'";
 $sql_result=mysql_fetch_array($result,MYSQL_BOTH);

 echo $sql_count=count($sql_result['name']);
?>

我收到以下错误:
错误:警告:mysql\u fetch\u array()期望参数1是resource,字符串在c:\xampp\htdocs\project1\config.php的第20行中给出
如何解决此问题?

yacmzcpb

yacmzcpb1#

你没有在代码中正确地分配三件事

  1. mysql_select_db($db_name) :这里您需要通过 $db_connect 2) 未执行查询: $result=mysql_query($sql_fetch) or die("Error: ".mysql_error()); 3) 查询结果需要传递给 mysql_fetch_array() 下面是经过上述更改的更新代码
$db_error='Establishing a Database Connection Error';

$localhost='localhost';

$db_user='root';

$db_name='uploader_db';

$db_pass='';

$db_connect=mysql_connect($localhost,$db_user,$db_pass) or die($db_error);

mysql_select_db($db_name,$db_connect) or die($db_error);

$sql_fetch="SELECT 'user','pass','name','email' FROM 'config' ORDER BY 'name'";

 $result=mysql_query($sql_fetch) or die("Error: ".mysql_error());

 $sql_result=mysql_fetch_array($result);

 echo $sql_count=count($sql_result['name']);

相关问题