2

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

Pradyut Bhattacharya
  • 5,440
  • 13
  • 53
  • 83

3 Answers3

5

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.

Marc B
  • 356,200
  • 43
  • 426
  • 500
4

You need to set client flags while connecting for using stored procedures with php. Use this:

mysql_connect($this->h,$this->u,$this->p,false,65536);

See MySQL Client Flags for more details. PHP MySQL does not allow you to run multiple statements in single query. To overcome this you must tell PHP to allow such queries by setting CLIENT_MULTI_STATEMENTS flag in your connection.

Ashwini Dhekane
  • 2,280
  • 14
  • 19
  • It should work. If it did not work. try calling mysql_connect again using flag 131072 or 199608. Which will set multiple results flag. – Ashwini Dhekane Apr 12 '11 at 22:38
1

I know last answer was a year ago, but...

CREATE PROCEDURE sp_sel_test(OUT yourscalarvariable INT/TEXT...)

Statements that return a result set cannot be used within a stored function. This includes SELECT statements that do not use INTO to fetch column values into variables, SHOW statements, and other statements such as EXPLAIN. For statements that can be determined at function definition time to return a result set, a Not allowed to return a result set from a function error occurs (ER_SP_NO_RETSET_IN_FUNC). For statements that can be determined only at runtime to return a result set, a PROCEDURE %s can't return a result set in the given context error occurs (ER_SP_BADSELECT).

So, your select shoould be like this:

       SELECT * FROM category **INTO** c;

http://www.cs.duke.edu/csl/docs/mysql-refman/stored-procedures.html