954,587 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Mysql display results

Ok I am stumped. I am inserting the data fine into the table and database so i know its not the config.php nor opendb.php. I used a code like this before and it work, i've only altered the $sql, the td's, and the $rows[]. Table name is hotspots (all lower case) all of the column names are lower case also. Column names are id, topic, comment, username, timestamp. Here's the code.

$sql="SELECT * FROM hotspots ORDER BY id DESC";
$result=mysql_query($sql);
 
 
$color="1";
 
echo '<table width="472px" border="0" align="center" cellpadding="2" cellspacing="0" style="border:1px solid #0000ff;">';
while($rows=mysql_fetch_array($result)){
 
if($color==1){
 
echo "<tr bgcolor='#6698FF' >
<td style='border-top:1px solid #0000ff; color:#000000;font-weight: bold;'>".$rows['username']."</td><td style='border-top:1px solid #0000ff; text-align: right;color:#000000;font-weight: bold;'>".$rows['timestamp']."</td></tr>
<tr bgcolor='#6698FF' style='border-bottom:1px solid #0000ff;color:#000000;font-weight: bold;'><td style=''>".$rows['topic']."</td><td style='text-align: right;color:#000000;font-weight: bold;'>".$rows['timestamp']."</td>
</tr><tr bgcolor='#6698FF'><td colspan='2' bgcolor='#6698FF'></td></tr><tr bgcolor='#6698FF'><td colspan='2' bgcolor='#6698FF'></td></tr><tr bgcolor='#6698FF'; style='border-bottom:1px solid #0000ff;' color:#000000;font-weight: bold;'><td colspan='2' style='color:#ffffff;font-weight: bold;font-size:13px;''>".$rows['comment']."</td>
</tr><tr bgcolor='#6698FF'><td style='border-bottom:1px solid #0000ff;' colspan='2' bgcolor='#6698FF'></td></tr>";
$color="2";
 
} else {
 
echo "<tr bgcolor='#AFDCEC'>
<td style='border-top:1px solid #0000ff;color:#0000ff;font-weight: bold;'>".$rows['username']."</td><td style='border-top:1px solid #0000ff; text-align: right;color:#0000ff;font-weight: bold;'>".$rows['timestamp']."</td></tr>
<tr bgcolor='#AFDCEC' style='border-bottom:1px solid #0000ff;color:#0000ff;font-weight: bold;'><td style=''>".$rows['topic']."</td><td style='text-align: right;color:#0000ff;font-weight: bold;'>".$rows['timestamp']."</td>
</tr><tr bgcolor='#AFDCEC'><td colspan='2' bgcolor='#AFDCEC'></td></tr><tr bgcolor='#AFDCEC'><td colspan='2' bgcolor='#AFDCEC'></td></tr><tr bgcolor='#AFDCEC' style='border-bottom:1px solid #0000ff;color:#0000ff;font-weight: bold;'><td colspan='2' style='color:#0000ff;font-weight: bold;font-size:13px;'>".$rows['comment']."</td>
</tr><tr bgcolor='#AFDCEC'><td style='border-bottom:1px solid #0000ff;' colspan='2' bgcolor='#AFDCEC'></td></tr>";
$color="1";
}

}
echo '</table>';
RazorRamon
Junior Poster in Training
74 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
 

Instead of assuming there are people on this forum who can simulate your server in their heads, could you maybe post what it is that is actually going wrong? Any errors?

Insensus
Junior Poster
112 posts since Mar 2011
Reputation Points: 70
Solved Threads: 46
 

Hi RazorRamon,

You really should put all that styling into a css file (or at the top of the document) to keep your code clean.

What is the actual problem you are having though? Are you getting any error messages?

edit: Sorry Insensus, I didn't notice your post as I type slow :)

Zagga
Posting Whiz in Training
257 posts since Dec 2009
Reputation Points: 22
Solved Threads: 54
 

This is the HTML code that calls the code above. I titled the code above viewcomments.php. I haven't uploaded this website, right now I am only testing it on wamp. There are no errors it just doesn't display what is in the table. I currently only have one entry in the table that I inserted using this HTML code and inserttopic.php that the form is directed to use. inserttable.php also uses config2.php and opendb.php to connect to the database which is why I know that is not the issue. Sorry Zagga I know the code is sloppy.

<html>

 
<body>
 
 

 


<DIV >
<table width="480" BACKGROUND="images/commentback.jpg">
     <tr>
         <td>             
             <form method="post" action="inserttopic.php">
              <span class="whitetitle">Topic: <input name="topic" type="text" STYLE="background: white; border: 1px #000000 solid;"><br >
              Username:<input name="username" type="text" STYLE="background: white; border: 1px #000000 solid;"><br >
	Comment:<br >
              <textarea name="comment" rows="4" cols="55" STYLE="background: white; border: 1px #000000 solid;">250 char. max</textarea><br >
              <input type="submit" value="send"></span>
              </form>
         </td>
     </tr>
</table>
</div>

 
<?php
include 'config2.php';
include 'opendb.php';
include 'viewcomments.php';
include 'closedb.php';
?>

 
 

</body>
 
 
</html>
RazorRamon
Junior Poster in Training
74 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
 

Are you sure the data is stored in the database correctly?

Try adding print_r($rows); to line 10 of viewcomments.php (after the while statement) to see if you are getting a result array.

Zagga
Posting Whiz in Training
257 posts since Dec 2009
Reputation Points: 22
Solved Threads: 54
 

I tried

print_r($rows);


no luck. I have a question though, do i need to set $rows to something before using it?

RazorRamon
Junior Poster in Training
74 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
 
$result= mysql_query("SELECT * FROM `hotspots` ORDER BY `id` DESC")or die (mysql_error());
$color="1";
echo '<table width="472px" border="0" align="center" cellpadding="2" cellspacing="0" style="border:1px solid #0000ff;">';
while($rows=mysql_fetch_array($result)){
if($color==1){
?>
<tr bgcolor='#6698FF' >
<td style='border-top:1px solid #0000ff; color:#000000;font-weight: bold;'><? echo $rows[username]; ?></td><td style='border-top:1px solid #0000ff; text-align: right;color:#000000;font-weight: bold;'><? echo $rows[timestamp]; ?></td></tr>
<tr bgcolor='#6698FF' style='border-bottom:1px solid #0000ff;color:#000000;font-weight: bold;'><td style=''><? echo $rows[topic]; ?></td><td style='text-align: right;color:#000000;font-weight: bold;'><? echo $rows[timestamp]; ?></td>
</tr><tr bgcolor='#6698FF'><td colspan='2' bgcolor='#6698FF'></td></tr><tr bgcolor='#6698FF'><td colspan='2' bgcolor='#6698FF'></td></tr><tr bgcolor='#6698FF'; style='border-bottom:1px solid #0000ff;' color:#000000;font-weight: bold;'><td colspan='2' style='color:#ffffff;font-weight: bold;font-size:13px;''><? echo $rows[comment]; ?></td>
</tr><tr bgcolor='#6698FF'><td style='border-bottom:1px solid #0000ff;' colspan='2' bgcolor='#6698FF'></td></tr>
<? 
$color="2";
}else{ ?>
<tr bgcolor='#AFDCEC'>
<td style='border-top:1px solid #0000ff;color:#0000ff;font-weight: bold;'><? echo $rows[username]; ?></td><td style='border-top:1px solid #0000ff; text-align: right;color:#0000ff;font-weight: bold;'><? echo $rows[timestamp]; ?></td></tr>
<tr bgcolor='#AFDCEC' style='border-bottom:1px solid #0000ff;color:#0000ff;font-weight: bold;'><td style=''><? echo $rows[topic]; ?></td><td style='text-align: right;color:#0000ff;font-weight: bold;'><? echo $rows[timestamp]; ?></td>
</tr><tr bgcolor='#AFDCEC'><td colspan='2' bgcolor='#AFDCEC'></td></tr><tr bgcolor='#AFDCEC'><td colspan='2' bgcolor='#AFDCEC'></td></tr><tr bgcolor='#AFDCEC' style='border-bottom:1px solid #0000ff;color:#0000ff;font-weight: bold;'><td colspan='2' style='color:#0000ff;font-weight: bold;font-size:13px;'><? echo $rows[comment]; ?></td>
</tr><tr bgcolor='#AFDCEC'><td style='border-bottom:1px solid #0000ff;' colspan='2' bgcolor='#AFDCEC'></td></tr>
<? 
$color="1";
}
}
echo'</table>';


Try that - I didn't see anything immediately wrong with it though.

public-image
Junior Poster in Training
76 posts since Oct 2010
Reputation Points: 10
Solved Threads: 11
 

$result is an array containing all the data returned that matched your query.
$rows runs through a while loop. It is an array that takes the first result of the query (1 row), then moves on to the next row, then the next until there are no more rows.

I haven't gone through your code thoroughly but it looks like it should work if you have some data to display.

If the print_r statement didn't display a result, it looks like nothing was retrieved from the database.

Zagga
Posting Whiz in Training
257 posts since Dec 2009
Reputation Points: 22
Solved Threads: 54
 

Check whether shorthand is enabled on your server otherwise....the code

<? echo $rows[username]; ?>

won't work....
If it is not enabled then write like this

<?php echo $rows[username]; ?>
IIM
Junior Poster
165 posts since Jun 2011
Reputation Points: 46
Solved Threads: 37
 

Great IIM!!! thanks it worked!!! thanks for all the help everybody..

RazorRamon
Junior Poster in Training
74 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
 

If your problem is solved mark the thread as solved!!!

IIM
Junior Poster
165 posts since Jun 2011
Reputation Points: 46
Solved Threads: 37
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: