-2

Possible Duplicate:
mysql_fetch_array() expects parameter 1 to be resource, boolean given in select

Hello please help me solve this problem

<?PHP

if(isset($_GET["id"]))
    {
        $id= $_GET["id"];}

        echo $id;


    $user_name = "root";
    $password = "";
    $database = "test";
    $server = "127.0.0.1";

$db_handle = mysql_connect($server, $user_name, $password);

$db_found = mysql_select_db($database, $db_handle);

if ($db_found) {


$SQL = "SELECT location FROM core_network WHERE sub_cat= (".$id.")";
   $r=mysql_query($SQL);
    $i=0;
    while($data=mysql_fetch_array($r))
    {
        $result[$i]=$data['location'];
        $i++;
    }
    #print_r($result);

echo $id; is printing normal and i can not find what is the problem please help me solve this thanks lot

Community
  • 1
  • 1

1 Answers1

2

That error is caused when your query fails. When mysql_query() fails, it returns a boolean false, as opposed to a resource, hence the error :

mysql_fetch_array() expects parameter 1 to be resource, boolean given

To debug, change you statement to

$r = mysql_query($SQL) or die(mysql_error());

PS - You should not be using mysql_*. Look into PDO. Here's a link to get you started: http://net.tutsplus.com/tutorials/php/php-database-access-are-you-doing-it-correctly/

xbonez
  • 40,730
  • 48
  • 157
  • 236
  • basically your query is failing to return any results. You should try it directly against your database and see if it's working there. If you want just do echo $SQL; die(); after $SQL = ... to get the exact query that's being uses. – Commander Nov 11 '12 at 02:22
  • hello the probles was the because of $id in the sub_cat= (".$id.")"; – Batnasan ganpurev Nov 11 '12 at 03:41
  • how to enter $id to sub_cat= (".$id.")" correctly? $id=cs_core if i write sub_cat='cs_core' it works, but sub_cat= (".$id.")" is not working can you advice for me? – Batnasan ganpurev Nov 11 '12 at 04:15
  • @Batnasanganpurev: since `sub_cat` is a `VARCHAR` field, you need to wrap its value in quotes. Change your query to `subcat=('" . $id . "')";` – xbonez Nov 11 '12 at 04:49
  • thanks a lot for your comment – Batnasan ganpurev Nov 11 '12 at 04:59
  • @Batnasanganpurev If that solved your problem, please go ahead and mark this as the correct answer (by clicking on the check mark) – xbonez Nov 11 '12 at 05:00