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
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;

Link to comment
Share on other sites

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

Link to comment
Share on other sites

<?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.

Link to comment
Share on other sites

<?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)) {
   echo $realtitles->post_title;
}
?>

 

try that.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

<?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.

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.