Jump to content

unable to use a returned $stmt object from a function to capture bound result


ajoo

Recommended Posts

Hi all !

 

I have a piece of code here:

$result = display_all(fcon, $var1, $var2);

function display_all( // defined in another file
$query = "SELECT one, two, three four, index1, index2 FROM numbers WHERE index1 = $var1 LIMIT 0, 1"; $result = mysqli_query($fcon, $query); return ($result); )

and then I use the returned variable $result to display the value as follows:-

		while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
		{
			echo "<tr>";
			echo "<td>".$one."</td>";
			echo "<td>".$two."</td>";
			echo "<td>".$three. "</td>";
			echo "<td>".$four. "</td>";
			echo "<td>".$index1. "</td>"; 
                        echo "</tr>";
               }    



and this displays the n rows of data returned.

 

 

 

Now I have started using prepared statements and the function is now 

function display_all{

      $query = "SELECT one, two, three four, index1 FROM numbers WHERE index1 = ?
                LIMIT 0, 10";
      $stmt = $conn->prepare($query);
      $stmt->bind_param('i',$var)
      if($stmt->execute())
      {
          $stmt->bind_result($one, $two, $three, $four, $index1);
          $stmt->store_result();
      }
return($stmt);
}

However the returned $stmt object is unable to display the n rows of data since it shows null values. I assume that this is not the right way to use the $stmt object to display data. I must be missing something. So I request you guys to help me with this.

 

Thanks loads.

Prepared statements don't work like this. Read the manual, especially the part about fetching data from a prepared statement.

 

Since you've decided to use MySQLi, you have to choose between two ugly solutions: Either you fetch() all rows within the function, stuff them into an array and return that array. Or you return the raw statement, in which case you have to call bind_result() and fetch() after your function has returned.

Hi Jacques, 

 

Thanks once again for this reply. I have just tried out the second method you described above. To use the raw stmt returned by the function. I still get nothing but a null array. 

 

Maybe I am misunderstanding what you mean by raw statement. here is what i did 

function display_all{

      $query = "SELECT one, two, three four, index1 FROM numbers WHERE index1 = ?
                LIMIT 0, 10";
      $stmt = $conn->prepare($query);
      $stmt->bind_param('i',$var)
      if($stmt->execute())
      {
          $stmt->bind_result($one, $two, $three, $four, $index1);
       }
return($stmt);
}

bound the result and returned $stmt.  And in the main 

$stmt->store_result();
$stmt->fetch();

but i still get null values. Kindly guide.

Thanks

Like I said, you need to do the bind_result() and fetch() outside of the function. In other words, the only job of your function is to create and execute the prepared statement.

 

After taking a closer look at your function, however, there are more problems. Where does $conn come from? Where does $var come from? You need to actually pass those variables to the function. Otherwise they're not available.

 

So your function needs to look like this:

<?php

function display_all($conn, $var)
{
	$stmt = $conn->prepare('
		SELECT whatever
		FROM idontcare
	');
	$stmt->bind_param('i', $var);
	$stmt->execute();
	
	return $stmt;
}

And you call it like this:

<?php

// $conn and $var need to be defined at this point
$all_entries = display_all($conn, $var);

$all_entries->bind_result($foo, $bar, $foobar);
while ($all_entries->fetch())
{
	// do something
}

I you have a chance to give up MySQLi and switch to PDO, consider doing that. It will make a lot of things easier, including this case.

Hi Jacques, 

 

Thanks for that once again. Such an oversight on my part. I thought it was store_result() instead of bind_result and fetch() that was to be executed outside the function. It has worked. 

 

Thanks for the advise on switching to PDO. I will keep that in mind but will reserve that for later. I have since your advice on filtered variables changed all files to mysqli prepared statements. I don't have the heart or the time right now to change it all once again. But I will keep it in mind.

 

My functions  is correct with the values being passed like you mentioned. 

 

Thanks very much ! Much obliged.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.