2012-04-23 89 views
-1

Possible Duplicate:
mysql_fetch_assoc error, can't seem to figure out what the problem is
Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in警告:mysql_fetch_assoc()预计参数1是resourc

<?php 
$Dblink = mysql_connect("localhost", "root", "","Amoozeshgah"); 
$res = mysql_query("select * from Tbl_About"); 
$rec = mysql_fetch_assoc($res); 


?> 

的查询工作,但有以下错误:

Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean 

为什么呢?

+0

可能重复,可以似乎找不出是什么问题](http://stackoverflow.com/q/2372029/),[警告:mysql_fetch_array():提供的参数不是一个有效的MySQL结果](http://stackoverflow.com/q/795746 /),或者当您提出问题并在“相关”部分中显示的其他几十人建议时H T。 – outis 2012-04-23 23:30:47

+0

在返回值部分阅读http://php.net/manual/en/function.mysql-query.php – Neverever 2012-04-23 23:31:40

+0

请勿使用['SELECT *'](http://stackoverflow.com/questions/321299/)除非您正在编写数据库管理程序;只选择你需要的列。 mysql扩展已过时,正在弃用。新代码应该使用mysqli或PDO,它们都具有重要的优点,例如支持预处理语句。 – outis 2012-04-23 23:32:53

回答

0

你混合mysql和mysqli的原料药的。更具体地说,您使用mysqli_connect()作为mysql_connect()。以下是解决问题的两种不同方法。

使用的MySQLi

$Dblink = mysqli_connect("localhost", "root", "","Amoozeshgah"); 
$res = mysqli_query($Dblink, "select * from Tbl_About"); 
$rec = mysqli_fetch_assoc($res); 

但是,如果你想继续使用MySQL的API,那么,你必须使用mysql_select_db()选择DB

$Dblink = mysql_connect("localhost", "root", ""); 
mysql_select_db("Amoozesh"); 
[mysql_fetch_assoc错误的
+0

在示例代码中的每个调用是mysql_ *函数 - 这是怎么混合的东西? – Cal 2012-04-23 23:32:46

+0

你不能看到'mysql_connect'代码吗? – Starx 2012-04-23 23:34:03

+0

tanx它的工作原理.. – 2012-04-23 23:38:39

-1

mysql_query()当语句失败时返回false - 打开display_errors找出原因。

我的第一个猜测是因为你还没有选择mysql_select_db()的数据库。你能证明你得到这样的错误:

$res = mysql_query("select * from Tbl_About"); 
if (!$res) die("Error: ".mysql_error()); 
+0

-1,这不是一个查询问题 – Starx 2012-04-23 23:34:24

相关问题