Jump to content

Pulling Data


rh91uk

Recommended Posts

Hiya

I need a bit of advice ... or maybe if some code if your feeling nice :)

I am writing a content management system, and in it, I have built a News System. But, the problem is, on the homepage I am adding a "Recent 3 News Posts", which will hopefully if I can get the concept together, pull the last 3 Added news stories.

I havent a clue how to do it though :). Can someone maybe point me in the right direction, or even if your feeling nice, write me a bit of code.

I would be greatful! :)

Thanks for help in advance,

Richard
Link to comment
Share on other sites

Sorry , should of been more clear.

We are using MYSQL.

Script wise - thats what I want to find out. I can pull the data from the database, but not thhe 3 most recent "ids". I dont know how to write the code to do it, as I am self taught, and hence never coming across this kind of situation...

The script is simple text boxes submitting to a Database (mysql).
Link to comment
Share on other sites

You can use this query which should select most recent 3 news posts:
[!--sql--][div class=\'sqltop\']SQL[/div][div class=\'sqlmain\'][!--sql1--][span style=\'color:blue;font-weight:bold\']SELECT[/span] * [color=green]FROM[/color] [color=orange]news_table[/color] [color=green]ORDER BY[/color] news_id [color=green]DESC[/color] LIMIT 3 [!--sql2--][/div][!--sql3--]
Obviously you'll need to change [b]news_table[/b] to the table that stores your news entries and [b]news_id[/b] to the news_id column name.
Link to comment
Share on other sites

[!--quoteo(post=386035:date=Jun 20 2006, 09:51 AM:name=wildteen88)--][div class=\'quotetop\']QUOTE(wildteen88 @ Jun 20 2006, 09:51 AM) [snapback]386035[/snapback][/div][div class=\'quotemain\'][!--quotec--]
You can use this query which should select most recent 3 news posts:
[!--sql--][div class=\'sqltop\']SQL[/div][div class=\'sqlmain\'][!--sql1--][span style=\'color:blue;font-weight:bold\']SELECT[/span] * [color=green]FROM[/color] [color=orange]news_table[/color] [color=green]ORDER BY[/color] news_id [color=green]DESC[/color] LIMIT 3 [!--sql2--][/div][!--sql3--]
Obviously you'll need to change [b]news_table[/b] to the table that stores your news entries and [b]news_id[/b] to the news_id column name.
[/quote]

Thank you wildteen88 :)
Link to comment
Share on other sites

[!--quoteo(post=386035:date=Jun 20 2006, 09:51 AM:name=wildteen88)--][div class=\'quotetop\']QUOTE(wildteen88 @ Jun 20 2006, 09:51 AM) [snapback]386035[/snapback][/div][div class=\'quotemain\'][!--quotec--]
You can use this query which should select most recent 3 news posts:
[!--sql--][div class=\'sqltop\']SQL[/div][div class=\'sqlmain\'][!--sql1--][span style=\'color:blue;font-weight:bold\']SELECT[/span] * [color=green]FROM[/color] [color=orange]news_table[/color] [color=green]ORDER BY[/color] news_id [color=green]DESC[/color] LIMIT 3 [!--sql2--][/div][!--sql3--]
Obviously you'll need to change [b]news_table[/b] to the table that stores your news entries and [b]news_id[/b] to the news_id column name.
[/quote]

Got another question

a) How do I convert the above statement in PHP, would:

[code]<?php
$query = "SELECT * FROM news_table ORDER BY news_id DESC LIMIT 3";
mysql_query($query, $mysql_link);
?>
[/code]

work?

b) How would I convert that query to display the Three Recent Titles, then linking it to the news story

c) How could I implement that in PHP

Sorry for my n00bness

Ric
Link to comment
Share on other sites

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]a) How do I convert the above statement in PHP, would:

[code]CODE
<?php
$query = "SELECT * FROM news_table ORDER BY news_id DESC LIMIT 3";
mysql_query($query, $mysql_link);
?>[/code]

work?[/quote]
Yes that'll work fine!

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]b) How would I convert that query to display the Three Recent Titles, then linking it to the news story?[/quote]
A bit like this

[code]<?php

//connect to do db here

$query = "SELECT news_id, news_title FROM news_table ORDER BY news_id DESC LIMIT 3";
$result = mysql_query($query, $mysql_link);

while($row = mysql_fetch_assoc($result))
{
    echo "<a href=\"news.php?id={$row['news_id']}\">{$row['news_title']}</a><br />";
}

?>[/code]
Again in this code, change any instance of news_id, news_table and news_title to your field/table names.
Link to comment
Share on other sites

Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /home/richard/public_html/noba/index.php on line 167

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/richard/public_html/noba/index.php on line 169

Any ideas? All DB connects are in the headers.....
Link to comment
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.