考虑以下代码:
<?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_fetch_array()期望参数1为资源,第20行的C:\ xampp \ htdocs \ Project1 \ config.php中给出的字符串 0
如何解决此问题?
答案 0 :(得分:0)
您没有在代码中正确分配三件事
1)mysql_select_db($ db_name):在这里您需要传递$ db_connect
2)不执行查询:$ result = mysql_query($ sql_fetch)或die(“ Error:” .mysql_error());
3)查询结果需要传递到mysql_fetch_array()
以下是具有上述更改的更新代码
$ db_error ='建立数据库连接错误';
$ localhost ='localhost';
$ db_user ='root';
$ db_name ='uploader_db';
$ db_pass ='';
$ db_connect = mysql_connect($ localhost,$ db_user,$ db_pass)或die($ db_error);
mysql_select_db($ db_name,$ db_connect)或die($ db_error);
$ sql_fetch =“ SELECT'user','pass','name','email'FROM'config'ORDER BY'name'”;
$ result = mysql_query($ sql_fetch)或die(“ Error:” .mysql_error());
$ sql_result = mysql_fetch_array($ result);
echo $ sql_count = count($ sql_result ['name']);