doujian0265 2014-05-31 08:20
浏览 57
已采纳

php select prepared语句是什么样的?

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
", 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!

  • 写回答

2条回答 默认 最新

  • duandang2123 2014-05-31 08:53
    关注
    // 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 }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?