Jump to content

Archived

This topic is now archived and is closed to further replies.

BillyBoB

help?

Recommended Posts

help me out here i need to retrieve data from a database. its the news and i need to display it in desc order
so the most recent comes first and i need to make sure there isnt more than 5 posts on one page

the table name is "news"
these are the fields:
title
poster
whenposted
textmessage

if u could just start me off i usually can think up this stuff but im drawing blanks :(

Share this post


Link to post
Share on other sites
[code]$result = mysql_query("SELECT * FROM news ORDER BY whenposted DESC LIMIT 5",$yourConnection);
while($row = mysql_fetch_assoc($result))
{
//Your display code 
echo('Title'.$row['title'].' by '.$row['poster'].'<br />');
echo($row['textmessage'].'<br />');
//ETC...
}
[/code]

Hope it helps.

Share this post


Link to post
Share on other sites
would u explain this part a bit i dont get what your doing with the $row
[code]
echo('Title'.$row['title'].' by '.$row['poster'].'<br />');
echo($row['textmessage'].'<br />');
[/code]

Share this post


Link to post
Share on other sites
$row is an array. The array was created with this bit of code:
[code]while($row = mysql_fetch_assoc($result))[/code]

title, poster and textmessage is the name of your field names within your table that you are querying for results.

Share this post


Link to post
Share on other sites
$row is fetching the array from the database. The mysql_fetch_array() function is what pulls the information out.

The $row['title'] part is where you are telling it from which column to take it. You're using the method MYSQL_ASSOC (associative, as opposed to numeric) so you call the array based on the name of the column.

Share this post


Link to post
Share on other sites
i put in this bit of code
[code]
<?php
$result = mysql_query("SELECT * FROM news ORDER BY whenposted DESC LIMIT 5")
while($row = mysql_fetch_assoc($result))
{
$newtime = $row['whenposted']
$time = gmdate("H:i:s m/d/Y", $newtime);

echo("<center>.$row['title'].<br>.' by '.$row['poster'].' on '.$time.<br>")
echo("$row['textmessage'].<br><hr></center>")
}
?>
[/code]

but got this error

Parse error: syntax error, unexpected T_WHILE in /home/dreamsh/public_html/test/index.php on line 103

Share this post


Link to post
Share on other sites
Missing semi-colon at the end of this line:
[code]<?php $result = mysql_query("SELECT * FROM news ORDER BY whenposted DESC LIMIT 5") ?>[/code]
These two lines:
[code]<?php
echo("<center>.$row['title'].<br>.' by '.$row['poster'].' on '.$time.<br>")
echo("$row['textmessage'].<br><hr></center>")
?>[/code]
Need to be written as:
[code]<?php
echo '<center>' . $row['title'] . '<br>by ' . $row['poster'] . ' on ' . $time . '<br>';
echo $row['textmessage'] . '<br><hr></center>';
?>[/code]

Ken

Share this post


Link to post
Share on other sites
yes its in a config.php file and at the top of this page has
[code]
<?php
include("config.php")
?>
[/code]

now that should of fixed it but this is the error

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/dreamsh/public_html/test/index.php on line 107

[code]
<?php
$result = mysql_query("SELECT * FROM news ORDER BY whenposted DESC LIMIT 5");
while($row = mysql_fetch_assoc($result)){
$newtime = $row['whenposted'];
$time = gmdate("H:i:s m/d/Y", $newtime);

echo(" '<center>'.$row['title'].'<br>'.' by '.$row['poster'].' on '.$time.'<br>' ");
echo("$row['textmessage'].'<br>'.'<hr>'.'</center>'");
}
?>
[/code]

Share this post


Link to post
Share on other sites
Fix those two echo lines like I said in my previous post.

Ken

Share this post


Link to post
Share on other sites
You are missing the ; again at the end of [b]include("config.php")[/b]

Also, your 107/108 line code should be:
[code]
echo("<center>$row['title']<br /> by $row['poster'] on $time <br />"); //this is line 107
echo("$row['textmessage'] <br /><hr></center>");[/code]

Share this post


Link to post
Share on other sites
<?php
include("config.php");
?>

Dont forget the semi-colon, I'm not sure it matters in this case above, but it's a good habbit.

Share this post


Link to post
Share on other sites
ok heres the latest

[code]
<?php
$result = mysql_query("SELECT * FROM news ORDER BY whenposted DESC LIMIT 5");
while($row = mysql_fetch_assoc($result)){
$newtime = $row['whenposted'];
$time = gmdate("H:i:s m/d/Y", $newtime);

echo("<center>$row['title']<br /> by $row['poster'] on $time <br />");//line 107
echo("$row['textmessage'] <br /><hr></center>");
}
?>
[/code]

still this error

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/dreamsh/public_html/test/index.php on line 107

Share this post


Link to post
Share on other sites
Those two echo lines need to be changed to:
[code]<?php
echo '<center>' . $row['title'] . '<br>by ' . $row['poster'] . ' on ' . $time . '<br>';
echo $row['textmessage'] . '<br><hr></center>';
?>[/code]

I posted this back in reply #8

Ken

Share this post


Link to post
Share on other sites
thx so much

this was a great learning experince which make me a lvl 50 coder lol

Share this post


Link to post
Share on other sites
Just remember to put semi-colons at the end of each line when appropriate because it'll always spit out errors.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.