简体   繁体   English

php select准备好的语句是什么样的?

[英]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 好吧..我已经在网上闲逛了几个小时,试图弄清楚如何转换我的旧mysql

Here is my php code atm 这是我的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 ?>

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. 我编写了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();
}

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 请检查您是否可以在此处使用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