BillyBoB Posted July 13, 2006 Share Posted July 13, 2006 help me out here i need to retrieve data from a database. its the news and i need to display it in desc orderso the most recent comes first and i need to make sure there isnt more than 5 posts on one pagethe table name is "news"these are the fields:titleposterwhenpostedtextmessageif u could just start me off i usually can think up this stuff but im drawing blanks :( Link to comment https://forums.phpfreaks.com/topic/14496-help/ Share on other sites More sharing options...
ShogunWarrior Posted July 13, 2006 Share Posted July 13, 2006 [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. Link to comment https://forums.phpfreaks.com/topic/14496-help/#findComment-57391 Share on other sites More sharing options...
BillyBoB Posted July 13, 2006 Author Share Posted July 13, 2006 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] Link to comment https://forums.phpfreaks.com/topic/14496-help/#findComment-57392 Share on other sites More sharing options...
BillyBoB Posted July 13, 2006 Author Share Posted July 13, 2006 ne body i know u can help Link to comment https://forums.phpfreaks.com/topic/14496-help/#findComment-57398 Share on other sites More sharing options...
wildteen88 Posted July 13, 2006 Share Posted July 13, 2006 $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. Link to comment https://forums.phpfreaks.com/topic/14496-help/#findComment-57401 Share on other sites More sharing options...
pixy Posted July 13, 2006 Share Posted July 13, 2006 $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. Link to comment https://forums.phpfreaks.com/topic/14496-help/#findComment-57402 Share on other sites More sharing options...
BillyBoB Posted July 13, 2006 Author Share Posted July 13, 2006 ok now i get lol srry i am still kinda tired i had a Mountain Dew Link to comment https://forums.phpfreaks.com/topic/14496-help/#findComment-57404 Share on other sites More sharing options...
BillyBoB Posted July 13, 2006 Author Share Posted July 13, 2006 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 errorParse error: syntax error, unexpected T_WHILE in /home/dreamsh/public_html/test/index.php on line 103 Link to comment https://forums.phpfreaks.com/topic/14496-help/#findComment-57412 Share on other sites More sharing options...
kenrbnsn Posted July 13, 2006 Share Posted July 13, 2006 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]<?phpecho("<center>.$row['title'].<br>.' by '.$row['poster'].' on '.$time.<br>")echo("$row['textmessage'].<br><hr></center>")?>[/code]Need to be written as:[code]<?phpecho '<center>' . $row['title'] . '<br>by ' . $row['poster'] . ' on ' . $time . '<br>';echo $row['textmessage'] . '<br><hr></center>';?>[/code]Ken Link to comment https://forums.phpfreaks.com/topic/14496-help/#findComment-57415 Share on other sites More sharing options...
BillyBoB Posted July 13, 2006 Author Share Posted July 13, 2006 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 errorParse 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] Link to comment https://forums.phpfreaks.com/topic/14496-help/#findComment-57418 Share on other sites More sharing options...
kenrbnsn Posted July 13, 2006 Share Posted July 13, 2006 Fix those two echo lines like I said in my previous post.Ken Link to comment https://forums.phpfreaks.com/topic/14496-help/#findComment-57420 Share on other sites More sharing options...
ShogunWarrior Posted July 13, 2006 Share Posted July 13, 2006 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 107echo("$row['textmessage'] <br /><hr></center>");[/code] Link to comment https://forums.phpfreaks.com/topic/14496-help/#findComment-57422 Share on other sites More sharing options...
pixy Posted July 13, 2006 Share Posted July 13, 2006 <?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. Link to comment https://forums.phpfreaks.com/topic/14496-help/#findComment-57424 Share on other sites More sharing options...
BillyBoB Posted July 13, 2006 Author Share Posted July 13, 2006 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 107echo("$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 Link to comment https://forums.phpfreaks.com/topic/14496-help/#findComment-57428 Share on other sites More sharing options...
kenrbnsn Posted July 13, 2006 Share Posted July 13, 2006 Those two echo lines need to be changed to:[code]<?phpecho '<center>' . $row['title'] . '<br>by ' . $row['poster'] . ' on ' . $time . '<br>';echo $row['textmessage'] . '<br><hr></center>';?>[/code]I posted this back in reply #8Ken Link to comment https://forums.phpfreaks.com/topic/14496-help/#findComment-57430 Share on other sites More sharing options...
BillyBoB Posted July 13, 2006 Author Share Posted July 13, 2006 thx so much this was a great learning experince which make me a lvl 50 coder lol Link to comment https://forums.phpfreaks.com/topic/14496-help/#findComment-57434 Share on other sites More sharing options...
pixy Posted July 13, 2006 Share Posted July 13, 2006 Just remember to put semi-colons at the end of each line when appropriate because it'll always spit out errors. Link to comment https://forums.phpfreaks.com/topic/14496-help/#findComment-57436 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.