MySQL错误:mysql_fetch_array期望参数1是资源[重复]

MySQL错误:mysql_fetch_array期望参数1是资源[重复]

问题描述:

Possible Duplicate:
Warning: mysql_fetch_* expects parameter 1 to be resource, boolean given error

I recently got a work from a client, where I have to change the depreciated php old code to new one. In that code I came across mysql_db_query which I converted into mysql_query but the error was given

mysql_fetch_array expects parameter 1 to be resource, boolean given

//the old code was like:

$result = mysql_db_query($mysql_db,"SELECT Hierarchy FROM MenuSystem WHERE LENGTH(Hierarchy) >= 2 AND LOCATE(" . $_SESSION['AccessLevel'] . ",AccessLevels) <> 0;");

//and my new code is :

$result = mysql_query("SELECT Hierarchy FROM MenuSystem WHERE LENGTH(Hierarchy) >= 2 AND LOCATE(" . $_SESSION['AccessLevel'] . ",AccessLevels) <> 0");

please tell me the problem

it refers to line number 11 where is the end bracket of while loop

while ($row = mysql_fetch_array($result)) {
    $ConcatHierarchy .= $row["Hierarchy"];
}

可能重复: strong>
警告:mysql_fetch_ *期望参数1为资源,布尔值 给出错误 p> blockquote>

我最近从客户端获得了一项工作,我必须将折旧的php旧代码更改为新代码。 在那段代码中,我遇到了mysql_db_query,我将其转换为mysql_query但错误已经给出了 p>

mysql_fetch_array期望参数1为资源,布尔给定 p>

  //旧代码如下:
 
 $ result = mysql_db_query($ mysql_db,“SELECT Hierarchy FROM MenuSystem WHERE LENGTH(Hierarchy)&gt; = 2 AND LOCATE(”。$ _SESSION ['AccessLevel']。“  ,AccessLevels)&lt;&gt; 0;“); 
 
 //和我的新代码是:
 
 $ result = mysql_query(”SELECT Hierarchy FROM MenuSystem WHERE LENGTH(Hierarchy)&gt; = 2 AND LOCATE(  “。$ _SESSION ['AccessLevel']。”,AccessLevels)&lt;&gt; 0“); 
  code>  pre> 
 
 

请告诉我问题 p>

它指的是第11行,其中是while循环的结束括号 p>

  while($ row = mysql_fetch_array($ result)){
 $ ConcatHierarchy。  = $ row [“Hierarchy”]; 
} 
  code>  pre> 
  div>

I can't see that the sole code will cause error. However, if you want to use $result as an array, that may rise a problem as we need to check it first. If query fails it will return a FALSE rather than a resource i.e., an array.

(I mean you use the resource later as an array actually. But yes $result is either a FALSE or a resrouce which can be used by calling mysql_fetch_array(). thanks for your comment)

Actually you can check other posts such as: mysql_fetch_array() expects parameter 1 to be resource problem

fyi, as suggested by the manual, ppl may prefer PDO now: Use of this extension is discouraged. Instead, the MySQLi or PDO_MySQL extension should be used.

You're doing a string operating, on something that's lacking quotes to make it into a string:

... AND LOCATE(" . $_SESSION['AccessLevel'] . ",AccessLevels) <> 0");
               ^--                             ^---

the indicated points require single quotes ('). Right now you're probably embedding some string, like 'SuperUser' in there. Without the quotes, that appears to MySQL as a field name, which almost certainly doesn't exist.

if you'd had appropriate error handling in your code, you'd have seen this immediately from the error message:

$result = mysql_query(...) or die(mysql_error());
                          ^^^^^^^^^^^^^^^^^^^^^^--- bare minimum