Jump to content

php grabbing from mysql help...


pkhandelwal

Recommended Posts

hello all,

 

So I was trying to use PHP to grab from a mysql database (a wordpress one, for those who are curious).  I feel like my code makes sense, but it isn't outputting anything.  help?

 

<?php

$titles = mysql_query("SELECT * FROM `wp_posts` LIMIT 0, 3");

$realtitles = mysql_fetch_object($titles);

echo $realtitles;

?>

Link to comment
https://forums.phpfreaks.com/topic/43695-php-grabbing-from-mysql-help/
Share on other sites

<?php

$titles = mysql_query("SELECT * FROM `wp_posts` LIMIT 0, 3");

$realtitles = mysql_fetch_object($titles);

echo $realtitles;

?>

 

Try this....

 


$sql_titles = "SELECT * FROM `wp_posts` LIMIT 0, 3";
$titles = mysql_query($sql_titles);
$realtitles = mysql_fetch_object($titles);
echo $realtitles;

so I used what you guys said and this is the new code:

 

<?php

$sql_titles = "SELECT * FROM `wp_posts` LIMIT 0, 3";

$titles = mysql_query($sql_titles);

$realtitles = mysql_fetch_object($titles);

echo $realtitles->post_title;

?>

 

The only thing is it only recalled one title, and at that it recalled the oldest title.  I was trying to recall the newest 3 titles.  Any other ideas?  Would greatly appreciate...

<?php
$sql_titles = "SELECT * FROM `wp_posts` ORDER BY date DESC LIMIT 0, 3";
$titles = mysql_query($sql_titles);
$realtitles = mysql_fetch_object($titles);
echo $realtitles->post_title;
?>

 

Where date is your date/time column or date is the id column.

Thanks a bunch.  My output is 'Test 3Test 2Test' which is what it should be.  Does anyone have any formatting tips on how I can get the output to come as a list? example:

 

test 3

test 2

test

 

?  In the end what I'm trying to do is have each of these headlines that are output act as links to those articles.  Is that overly complicated or is that doable?  If so, where do I start, and how do I go about doing it?

 

Sorry I know I'm being a hassle, but I've spent days and I've had no progress.

<?php
$sql_titles = "SELECT * FROM `wp_posts` ORDER BY date DESC LIMIT 0, 3";
$titles = mysql_query($sql_titles);
while($realtitles = mysql_fetch_object($titles)) {
     $url = $realtitles->post_url;
     $title = $realtitles->post_title;
     echo "<h1><a href=\"url\">$title</a></h1><br />\n";
}
?>

 

Or something to that effect. Make sure $url is getting set to the value desired.

Archived

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

×
×
  • 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.