2

I have a php code with 4 Menus(products,suppliers,purchases,customers). Each menu has its own stored procedure. EACH meanu has these code:

Code for Products:

<?php
$sql=$mysqli->query("call selectproducts()");
$i=1;
while($row=mysqli_fetch_array($sql)){
    $id=$row['prodid'];
    $date=$row['prodname'];
    $item=$row['proddescription'];
    $qtyleft=$row['prodsupplier'];
    $qty_sold=$row['proddate'];
    $price=$row['prodprice'];
    $sales=$row['prodquantity'];
    if($i%2){
?>
<tr id="<?php echo $id; ?>" class="edit_tr">
<?php } else { ?>
<tr id="<?php echo $id; ?>" bgcolor="#f2f2f2" class="edit_tr">
<?php } ?>
<td class="edit_td"><span class="text"><?php echo $date; ?></span> </td>
<td><span class="text"><?php echo $item; ?></span> </td>
<td><span class="text"><?php echo $qtyleft; ?></span></td>
<td><span id="last_<?php echo $id; ?>" class="text">
<?php
   echo $qty_sold;
?>

My problem is whenever I tried to use stored procedure in other menus or even in 1 menu this error in other meanus (suppliers,purchases,customers):

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given

To make it short, If i included just even one(1) stored procedured, the error occurs. The only thing that everything will work is that when i use it in normal way (SELECT * from table).

What could be the problem?please help me..This is the only problem in my project.

Mysql Stored Procedure Code:

CREATE DEFINER=`root`@`localhost` PROCEDURE `selectproducts`()
begin
select * from products order by ProdID;
end

Here's the full code: http://jsfiddle.net/WKKD4/

Sorry I have no other place to put it on.

4

4 に答える 4

2

MySQL Procedure does not return result set. The way you can access results from a MySQL procedure is as below.

CREATE PROCEDURE selectproducts (OUT ver_param VARCHAR(25))
BEGIN
  # Set value of OUT parameter
  SELECT count(ProdId)  INTO ver_param from products;
END;

Now to access the output in your PHP script, execute below query immediatly after the procedure call.

$sql = $mysqli->query("call selectproducts(@productcount)");
$results = $mysqli->query ("select @productcount as COUNT");
$rows = mysqli_fetch_array($results);
于 2013-03-25T14:44:19.930 に答える
1

Try this one:

**

$sql->close();
$mysqli->next_result();

**

Place this after fetching the results and see the magic.

于 2013-03-28T07:53:41.227 に答える
0

Try this change on your code:

<?php

    $result = $mysqli->query("call selectproducts()");
    $i=1;
    while($row = $result->fetch_assoc()){
        $id=$row['prodid'];
        $date=$row['prodname'];
        $item=$row['proddescription'];
        $qtyleft=$row['prodsupplier'];
        $qty_sold=$row['proddate'];
        $price=$row['prodprice'];
        $sales=$row['prodquantity'];
        if($i%2){
    ?>
    <tr id="<?php echo $id; ?>" class="edit_tr">
    <?php } else { ?>
    <tr id="<?php echo $id; ?>" bgcolor="#f2f2f2" class="edit_tr">
    <?php } ?>
    <td class="edit_td"><span class="text"><?php echo $date; ?></span> </td>
    <td><span class="text"><?php echo $item; ?></span> </td>
    <td><span class="text"><?php echo $qtyleft; ?></span></td>
    <td><span id="last_<?php echo $id; ?>" class="text">
    <?php
       echo $qty_sold;
    ?>

It's a minor change but it should works.

--------EDITED---------

Just for debug, replace the code with this:

$rs = $mysqli->query("call selectproducts();");
while($row = $rs->fetch_object())
{
var_dump($row);
}

------------EDIT2----------------------

Continuing with the debuugging stuff....make a new php script named test.php....put this code in it:

 <?php
  include('db.php');

  $query = "call selectproducts()";

  if ($result = $mysqli->query($query)) {

      /* obtener array asociativo */
      while ($row = $result->fetch_assoc()) {
          echo $row['prodid']."------".$row["prodname"])."</br>";
      }

      /* liberar el resultset */
      $result->free();
  }


?>

If this shows your data in the right way, then your problem is not here. Can you post the result of this script please.

Saludos ;)

于 2013-03-25T14:43:02.430 に答える
0

You are getting back multiple results. The result of the SELECT as well as the result of the call.

To get to the 'next' result, you must call:

<?php

  // Assuming $connection is your MySQLI object

  while($connection->next_result()) $connection->store_result();

?>
于 2013-03-25T14:46:39.013 に答える