以下查询返回广告系列表中user_id为1的所有行:
SELECT * FROM campaign WHERE user_id=1
在测试的情况下,这是两个结果。我怎样才能从每一行回显一个set列。例如,我想从每个结果中回显campaign_name。我尝试了各种方法,但是没有成功。
我的最终目标是这样的:
<?php foreach($queryRow as $row) { ?>
<li>
<a>
<div>
<p><?php echo($row['campaign_name']); ?></p>
<p>
Description Text
</p>
</div>
</a>
</li>
<?php } ?>
我完全迷失了,所以如果我的预期结果完全失效,我会道歉......
答案 0 :(得分:1)
试试这个:
$qry = "SELECT * FROM campaign WHERE user_id=1";
$res = mysqli_query($conn, $qry);
if(mysqli_num_rows($res) > 0) // checking if there is any row in the resultset
{
while($row = mysqli_fetch_assoc($res)) // Iterate for each rows
{
?>
<li>
<a>
<div>
<p><?php echo($row['campaign_name']); ?></p>
<p>
Description Text
</p>
</div>
</a>
</li>
<?php
}
}
它将迭代结果集中的每一行。
答案 1 :(得分:0)
我查看了Oldskool友好建议的文档,并意识到我使用了错误的方法来创建数组。我改为使用mysqli_results类的fetch_all功能来创建一个包含结果的多维数组。然后我能够使用以下代码来回显结果:
<!DOCTYPE html>
<html>
<body>
<?php
include('inc/database_initiation.php');
//print_r($userCampaigns);
foreach ($userCampaigns as $row) {
//echo $row[2];
//echo $row[4];
echo $row['campaign_name'];
echo '<br>';
echo $row['promotion_coins'];
echo '<br>';
}
?>
</body>
</html>
include'inc / database_initiation如下'
<?php
session_start();
include_once 'userManagement/dbconnect.php';
if(!isset($_SESSION['userSession']))
{
header("Location: login.php");
}
$query = $MySQLi_CON->query("SELECT * FROM users WHERE user_id=".$_SESSION['userSession']);
$userRow=$query->fetch_array();
$campaignQuery = $MySQLi_CON->query("SELECT * FROM campaign WHERE user_id=1");
$userCampaigns = $campaignQuery->fetch_all(MYSQLI_ASSOC);
//$MySQLi_CON->close();
?>