0

I'm trying to access a MYSQL procedure from PHP with mysql_query and mysql_fetch_array but then after the procedure returns valid i receive the following errors on other queries.

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

My code:

function sql_fetch($sql){
   global $lang;

  $query = mysql_query($sql);

if($query === FALSE) {
   die(mysql_error()); // TODO: better error handling
}
while($row = mysql_fetch_array($query))
    $a[] = $row;
return $a;
}


$menus = sql_fetch("call GetTop4Products();"); // here returns all right
$news = sql_fetch("select * from news order by date_created desc limit 0,3"); // here returns error.

if instead of calling procedure i put the procedure sql code, everything runs smooth.

and the error i receive:

     Commands out of sync; you can't run this command now
Radu Vlad
  • 1,470
  • 2
  • 21
  • 37

2 Answers2

1

You are passing a wrong parameter in mysql_fetch_array. The query resource ($result) connection must be passed as a parameter. If you want to call a stored procedure, you must use mysqli_query instead of mysql_query.

How to call a MySQL stored procedure from within PHP code?

<?php
mysql_connect("localhost", "mysql_user", "mysql_password") or
    die("Could not connect: " . mysql_error());
mysql_select_db("mydb");

$result = mysql_query("SELECT id, name FROM mytable");

while ($row = mysql_fetch_array($result)) {
    printf("ID: %s  Name: %s", $row[0], $row[1]);  
}

mysql_free_result($result);
?>
Community
  • 1
  • 1
cardeol
  • 2,198
  • 16
  • 25
0

check for missing quotation in your variable declaration or calling part. in php quotation marks causes lot of headache

Sp0T
  • 286
  • 8
  • 23