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.

15
  • So it seems that your SQL is failing. What happens if you run that directly on your MySQL server? What shows up in the logs? Commented Mar 25, 2013 at 14:25
  • Sorry but I don't understand. I'm using xampp. Can you elaborate further? Commented Mar 25, 2013 at 14:28
  • and if you do this change: $sql=$mysqli->query("call selectproducts()"); $i=1; while($row=$mysqli->fetch_array($sql)){ Commented Mar 25, 2013 at 14:28
  • the method fetch_array should be called from the mysqli_result object Commented Mar 25, 2013 at 14:29
  • can you show the code of selectproducts()? Commented Mar 25, 2013 at 14:31

4 Answers 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);
Sign up to request clarification or add additional context in comments.

5 Comments

It returna values in first store procedure, when i use it again for other menus, it has errors.
Value is stored in mysql variable which is specific to current mysql session. Also when you execute another sql statement, variable value may be unset. Always fetch procedures return value after you call your procedure
I may not be around for long, can you please provide your code snippet?
I have posted the link in my post. Please check it out
If i'll follow your answer, I will be creating another procedure because the procedures are used in my vb6.
1

Try this one:

**

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

**

Place this after fetching the results and see the magic.

1 Comment

WOW!Brilliant..really really really brilliant!
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();

?>

Comments

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 ;)

3 Comments

The result of first menu is also the result of other menus. Same results in all menus.
Still has errors. I think you should see the whole code for clear understanding.
You are my only hope. Once all of the menus can call stored procedure and i'll be fine..Please help me until the end. I can send you th whole project if you want. Just this one please.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.