Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

call stored procedure using php

i have written a basic stored procedure using mysql

DELIMITER // 

CREATE PROCEDURE `sp_sel_test`()
BEGIN

    SELECT * FROM category c;

END// 
DELIMITER ;

now i m calling it from php

the php code is:

  <?php
    $txt = $_GET['id'];
    $name = $_GET['name'];
$con = mysql_connect("localhost","four","password");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("fourthes_a", $con);



//$result = mysql_query("select * from new_c where name like %". $name ."% or c_name like %" . $name . "% order by name asc;");


$result = mysql_query("call sp_sel_test()");
    if ($result === FALSE) {
    die(mysql_error());
}
while($row = mysql_fetch_array($result))
  {
  echo $row['category_id'] . " " . $row['c_name'];
  ?>
  <br />
  <?php
  }

mysql_close($con);
    echo $txt;

?> 

now its giving the error

PROCEDURE fourthes_a.sp_sel_test can't return a result set in the given context

like image 342
Pradyut Bhattacharya Avatar asked Mar 25 '26 01:03

Pradyut Bhattacharya


1 Answers

mysql_query() returns false when the query fails. You didn't check if your sproc query succeeded, so most likely you're passing that boolean FALSE to the fetch function, which is rightfully complaining.

Rewrite your code like this, as a bare mininum, for proper error handling:

$res = mysql_query('call sp_sel_test()');
if ($res === FALSE) {
    die(mysql_error());
}

Never ever assume a query succeeded. Even if the SQL syntax is perfect, there's far too many other reasons for a query to fail to NOT check if it worked.

like image 161
Marc B Avatar answered Mar 26 '26 17:03

Marc B