Jump to content

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


ajoo
Go to solution Solved by Jacques1,

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.

Edited by ajoo
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

  • Solution

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.

Edited by Jacques1
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.