pkhandelwal Posted March 21, 2007 Share Posted March 21, 2007 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; ?> Quote Link to comment Share on other sites More sharing options...
edawg Posted March 21, 2007 Share Posted March 21, 2007 <?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; Quote Link to comment Share on other sites More sharing options...
UTAlan Posted March 21, 2007 Share Posted March 21, 2007 With fetch_object you have to explicitly say which field you want to retrieve. i.e. <?php $row = mysql_fetch_object($result); /* this is valid */ echo $row->field; ?> Quote Link to comment Share on other sites More sharing options...
pkhandelwal Posted March 21, 2007 Author Share Posted March 21, 2007 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... Quote Link to comment Share on other sites More sharing options...
per1os Posted March 21, 2007 Share Posted March 21, 2007 <?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. Quote Link to comment Share on other sites More sharing options...
pkhandelwal Posted March 21, 2007 Author Share Posted March 21, 2007 but the code still only spits out one title (the newest one now), even though I told it to list three (as I put the limit). What should I do? Quote Link to comment Share on other sites More sharing options...
per1os Posted March 21, 2007 Share Posted March 21, 2007 <?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. Quote Link to comment Share on other sites More sharing options...
pkhandelwal Posted March 21, 2007 Author Share Posted March 21, 2007 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. Quote Link to comment Share on other sites More sharing options...
UTAlan Posted March 21, 2007 Share Posted March 21, 2007 <?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. Quote Link to comment Share on other sites More sharing options...
pkhandelwal Posted March 21, 2007 Author Share Posted March 21, 2007 if I don't have a static link at this point, is there a way to make a dynamic link to a page which will grab all the appropriate information for that specific title that you click on? Quote Link to comment Share on other sites More sharing options...
UTAlan Posted March 21, 2007 Share Posted March 21, 2007 <?php $title = $realtitles->post_title; $url = "dynamicpage.php?title=$title"; ?> <?php $title = $_GET['title']; $sql = mysql_query("SELECT * FROM tableName WHERE title = '$title'"); ?> Quote Link to comment Share on other sites More sharing options...
pkhandelwal Posted March 21, 2007 Author Share Posted March 21, 2007 if I wanted to add more conditions to the original code, could I do this: $sql_titles = "SELECT * FROM `wp_posts` WHERE post_status = publish ORDER BY ID DESC LIMIT 0, 5"; (i added the where post_status equals publish.) Quote Link to comment Share on other sites More sharing options...
UTAlan Posted March 21, 2007 Share Posted March 21, 2007 Yes, but make sure you use single quotes where necessary (WHERE post_status = 'publish' ORDER BY...) Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.