简体   繁体   English

从多行查询中一次选择一行?

[英]Select one row at a time from a query with multiple rows?

I have a query that selects 3 rows; 我有一个选择3行的查询; but I only want to display the one row at a time, use the data from that row, then move to the next row, then repeat. 但是我只想一次显示一行,使用该行中的数据,然后移至下一行,然后重复。 How would this be accomplished? 这将如何实现? Table has 4 items in it. 表中有4个项目。

PHP: 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);
?> 

Attempt to display: 尝试显示:

        <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++;
        ?>

Output of attempt: 尝试输出:

Test
 || 
7/14/13

News 1

Testing to see if this will work lalalalalla

NOTE: Tried to repeat the attempt to see if it would work, but it displayed same as output but with a second one that was a duplicate except said News 2 注意:尝试重复尝试以查看它是否可以工作,但显示的内容与输出相同,但第二个重复,但表示“新闻2”

Desired output look: 所需的输出外观:

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.

The problem is that you are overwriting your variables with each while() loop - 问题是您正在使用每个while()循环覆盖变量-

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

so when you try to echo $id , $title , $news_date , and $post , you will only get the last row data 因此,当您尝试回显$id$title$news_date$post ,您只会得到最后一行数据

you either need to add your html in the while loop - 您要么需要在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++;
}

or save them to an array 或将它们保存到数组

$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'];
}

and then access the array in your display 然后访问显示中的数组

    <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++;
    ?>

I used [$number-1] as I assume $number is starting at 1 , and arrays are 0 based 我使用[$number-1]因为我假设$number1开始,并且数组基于0


edit here are ways to echo your html. 编辑这里是回显html的方法。 It would be beneficial to visit the php documentation, as all of this is there. 访问php文档将是有益的,因为所有这些都已存在。 In these I set $number back to 0 , but you can change it back to 1 , and just change each of the $number -> $number-1 , and the $number+1 -> $number 在这些代码中,我将$number设置回0 ,但是您可以将其更改回1 ,只需更改$number > $number-1$number+1 > $number

using a for loop - http://php.net/manual/en/control-structures.for.php 使用for循环-http://php.net/manual/zh/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   
}
?>

using a foreach loop - http://php.net/manual/en/control-structures.foreach.php 使用foreach循环-http: //php.net/manual/zh/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   
}
?>

using a while loop - http://php.net/manual/en/control-structures.while.php 使用while循环-http://php.net/manual/zh/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++;
}
?>

each of the above was done by closing out of php, and writing the html, and then opening php again. 通过关闭php并编写html,然后再次打开php来完成上述每个操作。 You could also just stay in php, and echo the html - http://php.net/manual/en/function.echo.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>";
     }
    ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM