Forum Moderators: coopster

Message Too Old, No Replies

Pulling articles from database

How to?

         

tr8er8

12:18 am on Feb 6, 2008 (gmt 0)

10+ Year Member



Hi I have this script I have on my website, and I have it so it displays all the messages on a page, from the newest first, and each page has 10 articles. But what I want to do, on a seperate page, is have them be pulled directly from my database they are in, for each record, newest first. And I also only want it to display the first 5.

I just want it to display this, which is working on the other page that has all the scripts:

<a href="<?php print $article_path;?><?php print $default_id;?>" title="<?php print $default_title;?>"><?php print $default_title;?></a>

I'm not really sure on how to do this but if you know what I mean please help :)

eelixduppy

12:21 am on Feb 6, 2008 (gmt 0)



You are going to be doing this is a very similar way to how your other script is working. But instead of taking them 10 at a time, you are going to change the limit to just say 5:
Sample query:

SELECT * .... .... LIMIT 5

Obviously not the whole query, but you get the point.

You might also want to take a look at the Basics of extracting data from MySQL [webmasterworld.com] for additional information.

Oh, and Welcome to WebmasterWorld! :)

tr8er8

12:31 am on Feb 6, 2008 (gmt 0)

10+ Year Member



No thats not the thing, the other script pulls the information from a actions.php page.

$offset = $page * $posts_per_page - $posts_per_page;

$query = mysql_query("SELECT * FROM $mysql_posts_table ORDER BY id DESC
LIMIT $posts_per_page OFFSET $offset");

global $mysql_comments_table;
display('header.php');

display('pages.php');

// Previous
if($page > 1)
print '<a href="?page=' . ($page - 1) . '"></a> ';
else
print '';

// Direct links.
for($a = 1; $a <= $pages; $a++)
if($a == $page)
print "$a ";
else
print '<a href="?page=' . $a . '">' . $a . '</a> ';

// Next
if($page < $pages)
print '<a href="?page=' . ($page + 1) . '"></a>';
else
print '';
display('pages2.php');

while($row = mysql_fetch_array($query))
{
global $default_title;
global $default_content;
global $default_date;
global $default_id;
global $default_author;
global $default_section;

$default_title = $row['title'];
$default_content = $row['content'];
$default_date = $row['date'];
$default_id = $row['id'];
$default_author = $row['author'];
$default_section = $row['section'];

$default_title = stripslashes($default_title);
$default_content = stripslashes($default_content);
$default_author = stripslashes($default_author);
$default_section = stripslashes($default_section);

// Get the number of comments for this post.
$query_sava = mysql_query("SELECT id FROM $mysql_comments_table WHERE
post=$default_id");

global $default_comments;

display('default.php');
}

That is just a small sample but that is basicly what puts it on the other page. I'm not really sure on how to make it be able to go on the other page, I tried doing:

display('default.php', 'news.php');

But that still doesn't work.

eelixduppy

12:33 am on Feb 6, 2008 (gmt 0)



Please read the link I have provided you in my first response. As for the query itself, it would look more like the following based off the example you gave me:

$query = mysql_query("SELECT * FROM `insert_table_name` ORDER BY id DESC
LIMIT 5");

tr8er8

12:47 am on Feb 6, 2008 (gmt 0)

10+ Year Member



Ok this is confusing me, are you saying I have to reconnect to the db and then pull out the information I need?

eelixduppy

12:49 am on Feb 6, 2008 (gmt 0)



>> on a seperate page

If you want this on a separate page you are going to have to query this again. The code you have included above is for a pagination script. Since this is not what you want, you must send the same or similar query from a separate file and sort/echo them according to your needs.

tr8er8

1:56 am on Feb 6, 2008 (gmt 0)

10+ Year Member



Ok this is what I have, with the username, pw, etc.:

[code]$connection = mysql_connect($host,$user,$pass) or die (mysql_errno().": ".mysql_error()."<BR>");
mysql_select_db($dbname);

$query = mysql_query("SELECT * FROM $mysql_posts_table ORDER BY id DESC
LIMIT 5");

while($row = mysql_fetch_array($query))
{
global $default_title;
global $default_date;
global $default_id;
global $default_author;

$default_title = $row['title'];
$default_date = $row['date'];
$default_id = $row['id'];
$default_author = $row['author'];

$default_title = stripslashes($default_title);
$default_author = stripslashes($default_author);
}[/cpde]

It connects fine, but I get a:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/bfplanet/public_html/articles/themes/BF3Planet/news.php on line 13

This is line 13:
while($row = mysql_fetch_array($query))

Im not sure whats wrong. but then under that I try to actually get some information by putting <?php print $default_title;?> but nothing shows up except the error. Do I have to define a row or what? Im not sure what to do after this

eelixduppy

1:58 am on Feb 6, 2008 (gmt 0)



Try this to debug the program:

$query = mysql_query("SELECT * FROM $mysql_posts_table ORDER BY id DESC
LIMIT 5") or die(mysql_error());

tr8er8

2:33 am on Feb 6, 2008 (gmt 0)

10+ Year Member



Nope it does work. It says:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ORDER BY id DESC LIMIT 5' at line 1

tr8er8

2:34 am on Feb 6, 2008 (gmt 0)

10+ Year Member



Hey also do you have MSN or xfire or something so we can talk like instant message each other?