Jump to content

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 :(
Link to comment
https://forums.phpfreaks.com/topic/14496-help/
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.
Link to comment
https://forums.phpfreaks.com/topic/14496-help/#findComment-57391
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.
Link to comment
https://forums.phpfreaks.com/topic/14496-help/#findComment-57402
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
Link to comment
https://forums.phpfreaks.com/topic/14496-help/#findComment-57412
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
Link to comment
https://forums.phpfreaks.com/topic/14496-help/#findComment-57415
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]
Link to comment
https://forums.phpfreaks.com/topic/14496-help/#findComment-57418
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
Link to comment
https://forums.phpfreaks.com/topic/14496-help/#findComment-57428
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.