Page 1 of 1

Printing a table from mysql

Posted: Mon Oct 19, 2009 12:23 pm
by gimpact
Hello,

Exactly as the subject says. Here is what I did

Code: Select all

<?php
session_start();
 
include ("secure/db.php");
// Get email id of the player
$email = $_SESSION['email'];
 
// Get all user post
$query = mysql_query("SELECT * FROM post WHERE email = '$email'");
//Calculate total number of rows
$row=mysql_num_rows($query);
 
$post = mysql_fetch_array($query);
 
$serial_number = $post['serial_number'];
$oneword = $post['oneword'];
$post = $post['post'];
 
// If there is no previous post
if ($row == 0){
    $row = 1;
    $serial_number = 1;
    $oneword = "You have no words";
    $post = "You have no post";
}
?>
 

Code: Select all

$i = 0;
    while($i < $row){
        print "<table width='450' border='0' class='table'>";
        print   "<tr valign='top'>";
        print "<td width='108' scope='col'><div align='left'>".$oneword."</td>";
        print "<td width='332' scope='col'><div align='left'>".$post."</td>";
        print "</tr>";
        print "</table>";
        $i++;
    }
I have two post bearing the same email ID. My codes prints one of this post twice. Some one please tell me what, where I am going wrong? I would like to print all post in the database matching the email, one after another.

Thank you,

Re: Printing a table from mysql

Posted: Mon Oct 19, 2009 12:44 pm
by nshiell
Can you show us your output!
Have you run the query in the command line, or PHPMyAdmin etc?
Is your data ok?

Re: Printing a table from mysql

Posted: Mon Oct 19, 2009 7:29 pm
by gimpact
Thank you for replying. Lets say, I have 10 posts submitted by a particular user. I am trying to print all the post of the user in this order
post 1
post 2
post 3
....
post 10

but my codes does this, since there are a total of 10 post of that user, it picks up the first post and prints it 10 times, like this
post 1
post 1
post 1
.....
post 1

Re: Printing a table from mysql

Posted: Mon Oct 19, 2009 7:51 pm
by Mirge
gimpact wrote:Thank you for replying. Lets say, I have 10 posts submitted by a particular user. I am trying to print all the post of the user in this order
post 1
post 2
post 3
....
post 10

but my codes does this, since there are a total of 10 post of that user, it picks up the first post and prints it 10 times, like this
post 1
post 1
post 1
.....
post 1
Well, you're only fetching the first result.

http://www.php.net/mysql_query/
http://www.php.net/mysql_fetch_array/

You should be using a loop (ie: while(...)) to iterate through ALL results, instead of the first result.

IE:

while($post = mysql_fetch_array($query)) {
// display table here
}