Php 将MySQLi open语句替换为prepared语句

Php 将MySQLi open语句替换为prepared语句,php,mysqli,prepared-statement,Php,Mysqli,Prepared Statement,我知道有一些关于准备好的声明的信息,但没有一个是针对我的需要的。我对PHP和MySQLI非常陌生,所以当我开始构建我的站点时,我并不真正了解准备好的语句。因此,我在进入每个页面进行更新时都会更新这些内容 我最初有下面的代码片段,下面是我改为的代码,但它没有返回任何结果 $sqlCommand = "SELECT users.id, scout_profile.s_name_first,scout_profile.s_name_mid, scout_profile.s_name_last,

我知道有一些关于准备好的声明的信息,但没有一个是针对我的需要的。我对PHP和MySQLI非常陌生,所以当我开始构建我的站点时,我并不真正了解准备好的语句。因此,我在进入每个页面进行更新时都会更新这些内容

我最初有下面的代码片段,下面是我改为的代码,但它没有返回任何结果

    $sqlCommand = "SELECT users.id, scout_profile.s_name_first,scout_profile.s_name_mid, scout_profile.s_name_last, scout_profile.nationality, scout_profile.s_country, scout_profile.s_club_main, scout_profile.s_type, scout_profile.status, scout_profile.gender, users.signup, users.avatar, users.username  FROM scout_profile INNER JOIN users ON scout_profile.scout_id=users.id";
include_once("../php_includes/db_conx.php");


   $query = mysqli_query($db_conx,$sqlCommand) or die(mysqli_error($sqlCommand));   
    $count = mysqli_num_rows($query);
if($count >= 1){
    $i = 0; // for ads
    $adCode=""; // for ads
   while($row = mysqli_fetch_array($query,MYSQLI_NUM)){
            // Insert Adds and selected interval - Begin (also look for (for ads))
     $i++;
     if($i==2 || $i==4 ||$i ==6){
         $adCode = '<div class="card-body bg-warning m-2 p-1 add_text" style="height: 100px;">This is Ad Space from Ads server Im on ('.$i.')</div>';
     }else{
         $adCode="";
     }
      $sid = $row[0];
      $s_name_first = $row[1];
      $s_name_mid = $row[2];
      $s_name_last = $row[3];
-更新但不工作

//$fn_searchquery = $_POST['fn_searchquery'] ?? '';
    $sqlCommand = "SELECT users.id, scout_profile.s_name_first,scout_profile.s_name_mid, scout_profile.s_name_last, scout_profile.nationality, scout_profile.s_country, scout_profile.s_club_main, scout_profile.s_type, scout_profile.status, scout_profile.gender, users.signup, users.avatar, users.username  FROM scout_profile INNER JOIN users ON scout_profile.scout_id=users.id";
include_once("../php_includes/db_conx.php");


$statement = $db_conx->prepare($sqlCommand);
$statement->execute();
//$result = $statement->get_result()->fetch_all(MYSQLI_ASSOC);
$result = $statement->get_result();
$statement -> store_result();
$count = $statement->num_rows;    

if($count >= 1){
    $i = 0; // for ads
    $adCode=""; // for ads

   while($row = $result->fetch_row()){
            // Insert Adds and selected interval - Begin (also look for (for ads))
     $i++;
     if($i==2 || $i==4 ||$i ==6){
         $adCode = '<div class="card-body bg-warning m-2 p-1 add_text" style="height: 100px;">This is Ad Space from Ads server Im on ('.$i.')</div>';
     }else{
         $adCode="";
     }
      $sid = $row[0];
      $s_name_first = $row[1];
      $s_name_mid = $row[2];
      $s_name_last = $row[3];

此代码不返回任何数据,如果必须,我可以更改为关联行,但会链接以保持其方式,因为更改的代码较少。我认为这与fetch函数有关,但我不确定调试它的最佳方法。

首先,您使用diemysqli_error$sqlCommand的第一个代码是不正确的。您需要使用db连接的变量,而不是查询声明语句。我看不出您在哪里回显任何内容。另外,你现在所拥有的,并不构成一份事先准备好的声明。可能值得一读,因为您可以同时使用这两种语言。您不需要为此查询准备语句。@Ashley,只需遵循我的教程:。它会回答你所有的问题。