首页 > 解决方案 > 带有 IN 子句的 PHP Prepared 语句不显示任何内容

问题描述

以下是我现在正在为比较网站测试的代码。但我无法使其与准备好的语句功能一起使用。如果我不使用准备好的语句方法,我的代码运行良好。表结构或使用foreach可能看起来不是正确的方法,但对于其他一些功能是必需的。

<?php
$dbname="ty465yu67u76i7";
$dbpass="jf54tg5y65y66u";
$dbuser="4t5t5y6y65u67u";
$hostname="localhost";
// SETP TWO : connecting to the database
$mysqli = new mysqli($hostname, $dbuser, $dbpass, $dbname);
$pid1="202882";
$userIdArray = [202882,202883,202884];
// number of question marks
$questionMarksCount = count($userIdArray);
// create a array with question marks
$questionMarks = array_fill(0, $questionMarksCount, '?');
// join them with ,
$questionMarks = implode(',', $questionMarks);
// data types for bind param
$dataTypes = str_repeat('i', $questionMarksCount);
// SETP THREE : SQL query
$results = $mysqli->prepare("SELECT * FROM phonespecs WHERE pid IN('.$questionMarks.')");
$results -> bind_param($dataTypes, ...$userIdArray);
$results -> execute();
$results -> store_result();

?>
<div class="basic-information">
<h3>Basic Information </h3>

<table class="table">
<tbody>

<tr>
<td>Models</td>
<?php foreach ($results as $result){ ?>
    <td> <strong><?php 
  if(!empty($result['name'])) {
   $mobname=$result['name'];
   } else{$mobname="Empty";}
   
   echo $mobname; ?></strong> </td>
<?php } ?>
</tr>


<tr>
<td>Manufacturer</td>
<?php foreach ($results as $result){ ?>
    <td> <strong><?php echo $result['brand']; ?></strong> </td>
<?php } ?>
</tr>

--以及更多表格数据--

标签: phpprepared-statement

解决方案


推荐阅读