【问题标题】:Select one row at a time from a query with multiple rows?从多行查询中一次选择一行?
【发布时间】:2013-07-15 16:16:40
【问题描述】:

我有一个选择 3 行的查询;但我只想一次显示一行,使用该行中的数据,然后移动到下一行,然后重复。这将如何实现?表中有 4 个项目。

PHP:

<?php
$server = "secret";
$user = "secret";
$pass = "secret";
$db = "secret";

$con=mysqli_connect($server,$user,$pass,$db);

if (mysqli_connect_errno($con))
   {
       echo "Failed to connect to MySQL: " . mysqli_connect_error();
   }

$sql = 'SELECT * FROM news ORDER BY id DESC LIMIT 3';

mysqli_select_db($con, $db);
$query = mysqli_query($con, $sql);
$number = 1;
while ($result = mysqli_fetch_array($query)) { 
    $id = $result['id'];
    $title = $result['title'];
    $news_date = $result['news_date'];
    $post = $result['post'];
}

mysqli_close($con);
?> 

尝试显示:

        <div name='title'><?php echo $title; ?></div> || <div name='news_date'><?php echo $news_date; ?></div>
        <p>News <?php echo $number; ?></p>
        <p><?php echo $post; ?></p>
        <?php   
           $number++;
        ?>

尝试的输出:

Test
 || 
7/14/13

News 1

Testing to see if this will work lalalalalla

注意:尝试重复尝试以查看它是否会起作用,但它显示的输出与输出相同,但第二个输出是重复的,除了所说的 News 2

所需的输出外观:

Newest Title | Newest Date
Newest news. Etc. Insert Big Paragraph of news here etc etc.

2nd Newest Title | 2nd Newest Date
2nd Newest news. Etc. Insert Big Paragraph of news here etc etc.

3rd Newest Title | 3rd Newest Date
3rd Newest news. Etc. Insert Big Paragraph of news here etc etc.

【问题讨论】:

  • 到目前为止您都尝试过什么,看来您现在需要处理这些结果。
  • 这是你的mysql查询,不是mysqli。
  • 事实上,每个示例都向您展示了如何“一次显示一行”。你试过了吗?
  • 看起来像duplicate。
  • Common Sense 另一个问题我措辞很糟糕,这是为了更好地措辞。我使用的查询是 MySQLI,它作为 MySQLI 工作。 Anigel,我已经尝试过 Your Common Sense 作为重复问题提出的问题,但那里的答案只重复显示第一行,而不是其他两个

标签: php html sql mysqli


【解决方案1】:

问题是你用每个while()循环覆盖你的变量-

while ($result = mysqli_fetch_array($query)) { 
    $id = $result['id'];
    $title = $result['title'];
    $news_date = $result['news_date'];
    $post = $result['post'];
}

所以当您尝试回显$id、$title、$news_date 和$post 时,您只会得到最后一行数据

您需要在 while 循环中添加您的 html -

while ($result = mysqli_fetch_array($query)) { 
    $id = $result['id'];
    $title = $result['title'];
    $news_date = $result['news_date'];
    $post = $result['post'];
    ?>

   <div name='title'><?php echo $title; ?></div> || <div name='news_date'><?php echo $news_date; ?></div>
    <p>News <?php echo $number; ?></p>
    <p><?php echo $post; ?></p>
    <?php   
       $number++;
}

或将它们保存到数组中

$id = array();
$title = array();
$news_date = array();
$post = array();

while ($result = mysqli_fetch_array($query)) { 
    $id[] = $result['id'];
    $title[] = $result['title'];
    $news_date[] = $result['news_date'];
    $post[] = $result['post'];
}

然后访问显示器中的数组

    <div name='title'><?php echo $title[$number-1]; ?></div> || <div name='news_date'><?php echo $news_date[$number-1]; ?></div>
    <p>News <?php echo $number; ?></p>
    <p><?php echo $post[$number-1]; ?></p>
    <?php   
       $number++;
    ?>

我使用了[$number-1],因为我假设$number 从1 开始,并且数组是基于0 的


编辑这里是回显您的 html 的方法。访问 php 文档会很有帮助,因为所有这些都在那里。在这些中,我将$number 设置回0,但您可以将其更改回1,只需更改每个$number->$number-1 和$number+1->$number

使用 for 循环 - http://php.net/manual/en/control-structures.for.php

<?php
for($number=0;$number<count($title);$number++){
?>
    <div name='title'><?php echo $title[$number]; ?></div> || <div name='news_date'><?php echo $news_date[$number]; ?></div>
    <p>News <?php echo $number+1; ?></p>
    <p><?php echo $post[$number]; ?></p>
<?php   
}
?>

使用 foreach 循环 - http://php.net/manual/en/control-structures.foreach.php

<?php
foreach($title as $number => $value{
?>
    <div name='title'><?php echo $title[$number]; ?></div> || <div name='news_date'><?php echo $news_date[$number]; ?></div>
    <p>News <?php echo $number+1; ?></p>
    <p><?php echo $post[$number]; ?></p>
<?php   
}
?>

使用 while 循环 - http://php.net/manual/en/control-structures.while.php

<?php
$number = 0;
while($number<count($title){
?>
    <div name='title'><?php echo $title[$number]; ?></div> || <div name='news_date'><?php echo $news_date[$number]; ?></div>
    <p>News <?php echo $number+1; ?></p>
    <p><?php echo $post[$number]; ?></p>
<?php   
    $number++;
}
?>

上面的每一个都是通过关闭php,编写html,然后再次打开php来完成的。你也可以留在 php 中,并回显 html - http://php.net/manual/en/function.echo.php

<?php
for($number=0;$number<count($title);$number++){
    echo "<div name='title'>".$title[$number]."</div> || <div name='news_date'>".$news_date[$number]."</div>";
    echo "<p>News ".($number+1)."</p>";
    echo "<p>".$post[$number]."</p>";
     }
    ?>

【讨论】:

  • 第二个会显示所有这些还是一个然后我必须调用另一个等等?
  • 第二个将全部显示,如果代码在循环中 - for($i=0;$i&lt;count($title);$i++){...display code...} 或 foreach($title as $key=&gt;$value){...display code...} 或 while($number&lt;=count($title){...display code...}
  • 您将如何正确地回显显示代码?抱歉,我不擅长回显 HTML
  • 谢谢您,我不知道您可以关闭并重新打开 php,但语句仍在运行。你教会了我很多东西,为此,非常感谢。
  • 没问题,很高兴为您提供帮助。快乐编码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-07-28
  • 1970-01-01
  • 2014-11-05
  • 1970-01-01
  • 2013-05-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多