我想从数据库中取出4个不同的id行。并以四个div显示。每个div包含一个字符库唯一ID。我这样做是为了一次又一次地写同一个查询。我想用较少的查询。 我的代码如下。
<?php
$query = mysql_query("select * from acms_pages where id = 40");
$sql = mysql_fetch_array($query);
?>
<li id="pgAbout">
<section class="content">
<div class="one_half_last"><?php echo $sql['text']; ?></div>
</section>
</li>
<section class="content">
<?php
$query = mysql_query("select * from acms_pages where id = 41");
$sql1 = mysql_fetch_array($query);
?>
<li id="pgPortfolio">
<div class="fullwidth"><?php echo $sql1['text']; ?></div>
</section>
</li>
<?php
$query = mysql_query("select * from acms_pages where id = 42");
$sql2 = mysql_fetch_array($query);
?>
<li id="pgServices">
<section class="content">
<div class="one_half_last"><?php echo $sql2['text']; ?></div>
</section>
</li>
<?php
$query = mysql_query("select * from acms_pages where id = 43");
$sql3 = mysql_fetch_array($query);
?>
<li id="pgPrices">
<section class="content">
<div class="one_half"><?php echo $sql3['text']; ?></div>
</section>
</li>
答案 0 :(得分:0)
这可能属于programmers.stackexchange.com,但一般的方法是将您的查询重写为单个查询,其中where子句包括所有四个条件OR'd。
然后你可以独立访问每一行并渲染div。
答案 1 :(得分:0)
要回答您的问题(我认为......),您可以在查询中使用OR:
mysql_query("select * from acms_pages where id = 40 OR id = 41 OR id = 42");
但是,您应该使用mysqli:
$link = mysqli_connect("localhost", "my_user", "my_password", "mydb");
if ($result = mysqli_query($link, "select * from acms_pages where id = 40 OR id = 41 OR id = 42"))
{
printf("Select returned %d rows.\n", mysqli_num_rows($result));
while ($row = mysqli_fetch_row($result)) {
printf ("%s", $row[0]); //first column value here
}
/* free result set */
mysqli_free_result($result);
}