1

I have some code which selects data from a row. It is working and prints the result correctly, but I have an error I can not figure out.

$rs=array();    
$select=array ("system_name", "location", "alarmtype", "severity", "start_time",     "end_time", "duration", "reason","shift_oparation", "system_oparation");

for ($i=0;$i<=9;$i++){
    $SQL = "SELECT (".$select[$i].") FROM (".$is.")  WHERE duration=('".$ic."') AND location=('".$id."')";
    $result = mysql_query($SQL);
    $db_field = mysql_fetch_array($result);
    $rs[$i]=$db_field[$select[$i]];
    echo $rs[$i];
}

Echo prints correctly, but there is an error:

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

Jon Adams
  • 24,464
  • 18
  • 82
  • 120
ganaa
  • 167
  • 2
  • 12
  • 2
    Why not use just one query? – G-Nugget Dec 01 '12 at 05:12
  • i have tried but but not working. i may write wrong. – ganaa Dec 01 '12 at 05:16
  • Possible duplicate [mysql_fetch_array() expects parameter 1 to be resource, boolean given in select](http://stackoverflow.com/questions/2973202/mysql-fetch-array-expects-parameter-1-to-be-resource-boolean-given-in-select?answertab=votes#tab-top) – John Conde Dec 30 '12 at 05:24

4 Answers4

1

I think some of your queries dont return anything. In order to get rid of the warning you should check if the $result was in fact a resource.

Try this:

$rs=array();    
$select=array ("system_name", "location", "alarmtype", "severity", "start_time",     "end_time", "duration", "reason","shift_oparation", "system_oparation");

for ($i=0;$i<=9;$i++){
$SQL = "SELECT (".$select[$i].") FROM (".$is.")  WHERE duration=('".$ic."') AND location=('".$id."')";
$result = mysql_query($SQL);

 if($result){
  $db_field = mysql_fetch_array($result);
  $rs[$i]=$db_field[$select[$i]];
  echo $rs[$i];
 }
}

However, your code will be a lot more efficient if you use only one query.

Teodor Talov
  • 1,933
  • 2
  • 25
  • 39
0

You are doing something really strange. It looks like $select is a list of columns in a table, and you are fetching them one column at a time!

You can select them all at once, with this query:

$sql = "SELECT system_name, location, alarmtype, severity, 
start_time, end_time, duration, reason, shift_oparation, system_oparation
FROM " . $is . " WHERE duration = '" . $ic . "' AND location = '" . $id . "'";

Note I've removed some extraneous parenthesis you had. Also be very careful with building SQL queries dynamically like this. I'm assuming you know the values of $is, $ic, and $id to be safe from SQL injection attacks,

Run the result, and then iterate over the row array

$result = mysql_query($sql);

if ($result)
{
    $row = mysql_fetch_array($result); //fetches the first result row as an array
                                       // Can be accessed like $row['system_name'] 

    foreach ($row as $key => $value)
        echo $key, " = ", $value;
}
else
    die(mysql_error() . $sql);

If the result is false, it will perform the die statement and print out the error returned from MySQL and also the query we built, so you can see if it looks correct.

akatakritos
  • 9,836
  • 1
  • 23
  • 29
0

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in.

This means that mysql query produced errors .

Return Values

For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error.

Use mysql_error to display teh error and see why it happened. e.g.

if(!$result){
   echo mysql_error();
  }

Protip: donlt use mysql libraries as noted in php manual

Elijan
  • 1,406
  • 1
  • 8
  • 11
0

there is problem in select statement please check the select query or you can run individual select query and check the output