简体   繁体   中英

What does a php select prepared statement look like?

Okay.. so ive been looking around on the web for hours now trying to figure out how to convert my old mysql

Here is my php code atm

$sql2="SELECT * FROM $tbl_name_question2 WHERE question_id='$question_id_comments' ORDER BY a_id ASC";
$result2=mysql_query($sql2);
// Comment Loop Starts
while($rows=mysql_fetch_array($result2)){
?>
<div class="row comment-body">
    <div class="col-md-3">
        <p><? echo $rows['a_name']; ?></p>
        <span><? echo $rows['a_datetime']; ?></span>
    </div>
    <div class="col-md-9">
        <p><? echo $rows['a_answer']; ?></p>
    </div>
</div>
<?php } // Comment Loop Ends ?>

I have the database connection info already setup properly as I wrote a MySQLi prepared statement to insert content which works, however I cannot figure this one out.

$datetime=date("m/d/y h:i"); // Format Date And Time

// Connect
$mysqli = new mysqli('private', 'private', 'private', 'private');

// Check Connection
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

If someone could show me in the correction direction or show me how to convert this it would be more than appreciated!

Huge thanks in advance!

// Prepare the statement, using ? in place of parameters
// Note that you can only use parameters where expressions are allowed, so the
// tablename must still be done by substituting a variable
$stmt = $mysqli->prepare("SELECT a_name, a_datetime, a_answer FROM $tbl_name_question2
                          WHERE question_id = ?
                          ORDER BY a_id ASC");
// Bind the parameters to the corresponding variables
$stmt->bind_param("s", $question_id_comments);
$stmt->execute();
// Bind variables to receive the results
$stmt->bind_result($name, $datetime, $answer);
// Fetch the rows, and use the above variables to output the results
while ($stmt->fetch() {
    ?>
    <div class="row comment-body">
        <div class="col-md-3">
            <p><? echo $name; ?></p>
            <span><? echo $datetime; ?></span>
        </div>
        <div class="col-md-9">
            <p><? echo $answer; ?></p>
        </div>
    </div>
    <?php }

Please check you can use mysqli code for this like example here

    <?php
    $mysqli = @new mysqli('private', 'private', 'private', 'private');

    if ($mysqli->connect_error) {
        printf("Connect failed: %s\n", $mysqli->connect_error);
    }

    /* create a prepared statement */
    if ($stmt = $mysqli->prepare("SELECT * FROM $tbl_name_question2 WHERE question_id=? ORDER BY a_id ASC")) {

    $stmt->bind_param('i',$question_id_comments);

    /* execute query */
    $stmt->execute();

    /* Store the result (to get properties) */
    $stmt->store_result();

    /* Get the number of rows */
    $num_of_rows = $stmt->num_rows;

    /* Get the result */
    $result = $stmt->get_result();

    while ($row = $result->fetch_assoc()) {
    ?>

    <div class="row comment-body">
        <div class="col-md-3">
            <p><?php echo $row['a_name']; ?></p>
            <span><?php echo $row['a_datetime']; ?></span>
        </div>
        <div class="col-md-9">
            <p><?php echo $row['a_answer']; ?></p>
        </div>
    </div>
    <?php
    }   

    }

    /* close statement */
    $stmt->close();
   }

   /* close connection */
   $mysqli->close();
   ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM