Php 准备SQL语句的过程性mysqli方法是什么?

Php 准备SQL语句的过程性mysqli方法是什么?,php,stored-procedures,mysqli,prepared-statement,procedural,Php,Stored Procedures,Mysqli,Prepared Statement,Procedural,我的代码中有一个SQL查询,我想将其转换为一个准备好的语句,以阻止SQL注入之类的漏洞。这就是我想要转换的: <?php $query = "SELECT * from `wp_posts` WHERE ID=$pid "; $result = mysqli_query($link, $query); //$id=$row['Gallery_Id']; while($row = mysqli_fetch_array($result)){ ?>

我的代码中有一个SQL查询,我想将其转换为一个准备好的语句,以阻止SQL注入之类的漏洞。这就是我想要转换的:

<?php
$query = "SELECT * from `wp_posts` WHERE ID=$pid ";
$result = mysqli_query($link, $query);
    //$id=$row['Gallery_Id'];

    while($row = mysqli_fetch_array($result)){
        ?>
    <h2 align="center"> <?php echo $row['post_title']; ?> </h2><br>
    <div class="paracenter">

        <p id="cont"><?php echo $row['post_content']; ?></p>
        <hr color="black" width="10%">

    </div>
<?php } ?>



这是我试过的,但不起作用

$query = "SELECT * from `wp_posts` WHERE ID=? ";
    $stmt = mysqli_prepare($link, $query);
    if($stmt){
        mysqli_stmt_bind_param($stmt, "i", $pid);
        mysqli_stmt_bind_result($stmt, $dbpid);
        mysqli_stmt_execute($stmt);
        mysqli_stmt_fetch($stmt);
    }
    $result = mysqli_query($link, $query);
    //$id=$row['Gallery_Id'];

    while($row = mysqli_stmt_fetch($result)){
        ?>


    <h2 align="center"> <?php echo $row['post_title']; ?> </h2><br>
    <div class="paracenter">

        <p id="cont"><?php echo $row['post_content']; ?></p>
        <hr color="black" width="10%">

    </div>
    <?php } ?>
$query=“从`wp_posts`中选择*ID=?”;
$stmt=mysqli\u prepare($link,$query);
如果($stmt){
mysqli_stmt_bind_param($stmt,“i”,$pid);
mysqli_stmt_bind_result($stmt,$dbpid);
mysqli_stmt_execute($stmt);
mysqli_stmt_fetch($stmt);
}
$result=mysqli\u查询($link,$query);
//$id=$row['Gallery_id'];
while($row=mysqli\u stmt\u fetch($result)){
?>



几乎所有在线示例都没有使用我使用的过程方法。我如何纠正这一点?

为了保护您的查询免受注入攻击,您有两种选择。第一种方法非常简单,与准备好的语句一样安全

  • $pid
    转换为整数

    $query = "SELECT post_title, post_content FROM wp_posts WHERE ID = " . (int)$pid;
    
    安全完成

  • 如何使用结果绑定编写准备好的语句…(我不使用过程mysqli语法)

    if(!$stmt=$link->prepare(“选择post\u title,post\u content FROM wp\u posts,其中ID=?”){
    echo“Syntax Error@Prepare”;/$link->Error;bind_参数(“i”,$pid)| |!$stmt->execute()| |!$stmt->bind_结果($title,$content)){
    echo“语法错误@ParamBind |执行| ResultBind”/$stmt->Error;fetch()){
    回声“;
    回显“$title
    ”; 回声“; echo“

    $content

    ”; 回声“”; 回声“; } }
  • 一些补充说明

    • 如果不打算使用结果绑定,则应使用
      mysqli\u fetch\u assoc()
      而不是
      mysqli\u fetch\u数组()
      mysqli\u fetch\u数组()
      将生成索引元素和关联键控元素的膨胀结果集(实际需要的两倍)
    • 使用
      bind_result()
      时,需要将SELECT子句中的
      *
      替换为要提取的列
    • 我的第一个
      elseif()
      表达式包含对
      $stmt
      的三个单独调用和检查。一旦其中任何一个调用返回错误响应,条件表达式就会短路,表达式中的其余调用将永远不会执行
    • 如果采用我的面向对象mysqli样式,请确保将数据库连接语法也与面向对象对齐

    该手册有OOP和过程风格。目前发生的情况是,那里的示例只是使用定义的变量进行输出。它没有提到当查询是
    选择*
    @chris85并将其应用于查询时如何获取值。你把它搞得乱七八糟,你在混合
    *i\u query()
    和准备好的报表api@Ghost,你能告诉我怎么改正吗?
    if (!$stmt = $link->prepare("SELECT post_title, post_content FROM wp_posts WHERE ID = ?")) {
        echo "Syntax Error @ Prepare"; // $link->error; <-- never show actual error details to public
    
    } elseif (!$stmt->bind_param("i", $pid) || !$stmt->execute() || !$stmt->bind_result($title, $content)) {
        echo "Syntax Error @ ParamBind | Execute | ResultBind"; // $stmt->error; <-- never show actual error details to public
    } else {
        while ($stmt->fetch()) {
            echo "<div>";
                echo "<h2 align=\"cente\">$title</h2><br>";
                echo "<div class=\"paracenter\">";
                echo "<p id=\"cont\">$content</p>";
                echo "<hr color=\"black\" width=\"10%\">";
            echo "</div> ";
        }
    }