简体   繁体   English

我如何在php中显示来自不同mysql行的内容?

[英]How would I display content from a different mysql row in php?

I'm sorry if the title might confuse you, it will make more sense with some context. 很抱歉,标题可能会使您迷惑,在某些情况下,它会更有意义。

In my mysql table I have rows all sorted by id and they have a place for the title, content and date. 在我的mysql表中,所有行均按ID排序,并且它们的标题,内容和日期都有位置。 In my php code I display the title to the post, using "echo $title" which works fine. 在我的PHP代码中,我使用“ echo $ title”显示帖子的标题,效果很好。 But on the same page I want to display the title to my 2nd row but when I use echo $title it shows the same title as the first row. 但是在同一页面上,我想显示第二行的标题,但是当我使用echo $ title时,它显示的标题与第一行相同。

If you need any more information I'd be glad to help. 如果您需要更多信息,我们将很乐意为您提供帮助。

To iterate over an array you need to use fetch the query as an associative array then loop over it. 要遍历数组,您需要使用将查询作为关联数组使用,然后对其进行循环。 This should let you control the values and parse them as you want them. 这应该使您可以控制值并根据需要解析它们。

http://us3.php.net/mysql_fetch_assoc http://us3.php.net/mysql_fetch_assoc

$queryData = mysql_fetch_assoc($query);
$title1 = $queryData[0]['title'];
$title2 = $queryData[1]['title'];

Have fun learning and exploring, PHP is a field of fun 有趣的学习和探索,PHP是一个有趣的领域

The problem is not clear that much. 问题不是很清楚。 But maybe, you want to show your all SQL data in a page. 但是也许您想在页面中显示所有SQL数据。 You already got first row's first data but want to seconds rows data? 您已经获得了第一行的第一数据,但想秒数据? or all data? 还是所有数据? If it is then you can just apply a loop to show the all data in a table. 如果是,那么您可以应用循环以显示表中的所有数据。 Here is the simple mysqli procedural PHP code. 这是简单的mysqli程序PHP代码。 You can also use PDO or Object oriented approach PHP code. 您还可以使用PDO或面向对象的方法PHP代码。

In show.php file 在show.php文件中

/*make database connection here. You have already did it. 
I assume that your connection variable name is $conn;*/

/*SQL query for show all data in tha *allposts* table */

   $sql_query = "SELECT * FROM allposts";
   $result = $conn->query($sql_query);
   <table>
     <tr>
        <th>Title</th>
        <th>Content</th>
        <th>Date</th>
     </tr>
    if ($result->num_rows > 0) {
     // output data of each row
      while($row = $result->fetch_assoc()) {
     <tr>
       <td>echo $row["title"];</td>
       <td>echo $row["content"];</td>
       <td>echo $row["date"]</td>
     </tr>        
     }
    } else {
      echo "0 results";
    }
</table>

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

相关问题 如何在PHP中显示MySQL数据库中的内容 - How do i display content from a MySQL DB in PHP 我如何将MYSQL行计入PHP中的特定行? - How would I count MYSQL rows down to a specific row in PHP? 当使用php和mysql从数据库列出信息时,你会如何使第一行看起来与其他行不同? - When listing information from a database using php and mysql how would you make the first row look different to the rest? 如何在一个php文件中显示数据库中的不同内容? - how do I display different content from database on one php file? 在特定表列上显示PHP在MySQL上的行内容(不同表上的.row名称相同) - Show MySQL row content on PHP from specific table column (same .row name on different tables) 如何从mysql PHP显示图像 - How to I display image from mysql PHP 如何使用PHP在行跨度中显示mysql数据? - How do I display mysql data in row span using PHP? MySQL / PHP如何在mysql中插入一行并将下面的所有自动增量字段增加1 - MySQL/PHP How would I insert an row into mysql and increase all the auto incremented fields below by 1 如何在php中的不同页面中显示数据库的每一行? - how to display each row from database in different page in php? 如何在PHP和MySQL中将行值分成不同的列 - How can I separate row values into different columns in PHP and MySQL
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM