0
votes

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. In my php code I display the title to the post, using "echo $title" which works fine. 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.

If you need any more information I'd be glad to help.

2
What he said ^^ - and if you want data from two rows, just read two rows.user1864610

2 Answers

0
votes

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

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

Have fun learning and exploring, PHP is a field of fun

0
votes

The problem is not clear that much. But maybe, you want to show your all SQL data in a page. 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. You can also use PDO or Object oriented approach PHP code.

In show.php file

/*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>