Jump to content

Multiple prepared statements


skyer2000

Recommended Posts

I can't figure out how to make another database call in the middle of this code to fetch more results:

 

<?
    if (mysqli_stmt_prepare($stmt, $sql)) {
        mysqli_stmt_bind_param($stmt, 'i', $topid);
        mysqli_stmt_execute($stmt);
        mysqli_stmt_bind_result($stmt, $fieldid, $fieldtype, $parentid, $fieldvalue);
        while(mysqli_stmt_fetch($stmt)) {
        echo '<p>';    
        echo '<label for="'.$fieldid.'">'.$fieldvalue.'</label><br />';
            if($fieldtype == '1') {
                echo '<input type="text" />';
            } elseif($fieldtype == '2') {
                echo '<select>';
                                 //there needs to be another database call here to pull in the option values for the select
                               echo '</select>';
            } elseif($fieldtype == '3') {
                echo '<textarea></textarea>';
            }
        echo '</p>';    
        }
    }
?>

 

With standard MySQL, I could just start another MySQL statement there. But now, what do I need to do to get multiple prepared statements running at the same time? Or are there different, better ways?

Link to comment
Share on other sites

Umm. The first parameter ($stmt in your posted code) holds the statement identifier. You use that in all the related prepared functions to reference which prepared statement they are to operate on. To use more than one prepared statement, use a different variable name to hold the statement identifier.

Link to comment
Share on other sites

Putting in the new statement identifier brings up the following errors:

 

$sql2 = 'NEW SQL STATEMENT';

$stmt2 = mysqli_stmt_init($connect);

 

mysqli_stmt_prepare($stmt2, $sql2);

mysqli_stmt_bind_param($stmt2, 'i', $papid);

mysqli_stmt_execute($stmt2);

mysqli_stmt_bind_result($stmt2, $stuff);

while (mysqli_stmt_fetch($stmt2)) {

$echo stuff;

}

mysqli_stmt_close($stmt2);

 

Warning: mysqli_stmt_bind_param() [function.mysqli-stmt-bind-param]: invalid object or resource mysqli_stmt in C:\location\functions_conf.php on line 177

 

Warning: mysqli_stmt_execute() [function.mysqli-stmt-execute]: invalid object or resource mysqli_stmt in C:\location\functions_conf.php on line 178

 

Warning: mysqli_stmt_bind_result() [function.mysqli-stmt-bind-result]: invalid object or resource mysqli_stmt in C:\location\functions_conf.php on line 179

 

Warning: mysqli_stmt_fetch() [function.mysqli-stmt-fetch]: invalid object or resource mysqli_stmt in C:\location\functions_conf.php on line 180

 

Warning: mysqli_stmt_close() [function.mysqli-stmt-close]: invalid object or resource mysqli_stmt in C:\location\functions_conf.php on line 183

Link to comment
Share on other sites

<?
    if (mysqli_stmt_prepare($stmt, $sql)) {
        mysqli_stmt_bind_param($stmt, 'i', $topid);
        mysqli_stmt_execute($stmt);
        mysqli_stmt_bind_result($stmt, $fieldid, $fieldtype, $parentid, $fieldvalue);
        while(mysqli_stmt_fetch($stmt)) {
        echo '<p>';   
        echo '<label for="'.$fieldid.'">'.$fieldvalue.'</label><br />';
            if($fieldtype == '1') {
                echo '<input type="text" />';
            } elseif($fieldtype == '2') {
                echo '<select>';
                                      $sql2 = 'WORKING SQL STATEMENT';
                                      $stmt2 = mysqli_stmt_init($connect);

                                      mysqli_stmt_prepare($stmt2, $sql2);
                                      mysqli_stmt_bind_param($stmt2, 'i', $papid);
                                      mysqli_stmt_execute($stmt2);
                                      mysqli_stmt_bind_result($stmt2, $stuff);
                                      while (mysqli_stmt_fetch($stmt2)) {
                                          $echo stuff;
                                      }
                                      mysqli_stmt_close($stmt2);
                               echo '</select>';
            } elseif($fieldtype == '3') {
                echo '<textarea></textarea>';
            }
        echo '</p>';   
        }
    }
?>

Link to comment
Share on other sites

I tried putting a regular MySQL statement in there:

 

$query = 'WORKING SQL STATEMENT';
$result = mysqli_query($connect, $query);
$row = mysqli_fetch_array($result, MYSQLI_BOTH);
echo $row['foo'];

 

This works anywhere outside of    while(mysqli_stmt_fetch($stmt)) { }

 

However, once placed in there it spits out the error:

 

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in C:\path\functions_conf.php on line 175

 

 

At the beginning I even tried a $connect2 = $connect, then tried using $connect2 instead. That didn't work either.

Link to comment
Share on other sites

I might of figured out the direction I need to go to solve my issue with multiple prepared statements. I've been doing some reading and ran into Subqueries in MySQL.

 

 

Here is the first MySQL statement, that just pulls up the information:

 

SELECT papers.papid AS papid,papers.title AS title,papers.authors AS authors, papers.status AS status_num 
FROM papers WHERE papers.topid = ?

 

The second query takes the papid, checks a different table, and finds all values that have the same papid

 

SELECT keywords.keyid AS keyid,keywords.keyword AS keyword 
FROM keywords WHERE keywords.papid = ?

 

 

How would I then combine those two into one query, then somehow pull only the paper info once, but the keywords as many times as I need? Or is that even possible?

Link to comment
Share on other sites

Ok so lets say I do this:

 

SELECT papers.papid AS papid,papers.title AS title, papers.authors AS authors, papers.status AS status_num, keywords.keyid AS keyid, keywords.keyword AS keyword
FROM papers,keywords
WHERE papers.topid = ?
AND keywords.topid = papers.topid

 

I then can echo all these values, however, keywords only displays the first keyword it found. How do I make it display multiple keywords (that it should have found with this query)

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.