簡體   English   中英

php select准備好的語句是什么樣的?

[英]What does a php select prepared statement look like?

好吧..我已經在網上閑逛了幾個小時,試圖弄清楚如何轉換我的舊mysql

這是我的PHP代碼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 ?>

我編寫了MySQLi准備的語句以插入有效的內容時,我已經正確設置了數據庫連接信息,但是我無法弄清楚這一點。

$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();
}

如果有人可以向我展示校正方向或向我展示如何進行轉換,那將不勝感激!

提前非常感謝!

// 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 }

請檢查您是否可以在此處使用mysqli代碼作為示例

    <?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();
   ?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM