sydewyndr Posted February 12, 2007 Share Posted February 12, 2007 Using the following code, I'm able to pull in all of the data that exists in "post_title." But I only need the newest one. <? $host='host'; $username='user'; $pass='pass'; $database='CTW_BM_db'; mysql_connect ($host, $username, $pass); mysql_select_db($database) or die('Could not connect to the Database'); $info = mysql_query("SELECT post_title FROM wp_posts"); while ($r = mysql_fetch_array($info)) { echo "<a href=the link>$r[post_title]<\/a>"; } ?> Can someone please help me so it only displays the title stored in the newest record? Also, I'm getting the following error messages at the top of my webpage: Table 'CTW_BM_db.client_module_customization' doesn't exist SELECT search, `replace` FROM client_module_customization WHERE client_module=50 AND active=1 And these error message at the bottom of my webpage: Table 'CTW_BM_db.page_view' doesn't exist SELECT ID FROM page_view WHERE month='2007-02' AND file_name='index' AND publication=5 AND client=1 AND source='module' Table 'CTW_BM_db.page_view' doesn't exist INSERT INTO page_view (month, file_name, publication, client, source, count) VALUES ('2007-02', 'index', 5, 1, 'module', 1) Quote Link to comment Share on other sites More sharing options...
Balmung-San Posted February 12, 2007 Share Posted February 12, 2007 In order to only get the latest you would need some sort of unique id field, then you'd just attached ORDER BY unique_id_field_name LIMIT 1 to the end of your query. Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 12, 2007 Share Posted February 12, 2007 This looks like a problem with wordpress. (If that's what it is) If you want the newest entry, it'd be something like SELECT post_title FROM wp_posts ORDER BY id DESC LIMIT 1 We'd need to know the fields in order to correctly specify the sorting. Quote Link to comment Share on other sites More sharing options...
sydewyndr Posted February 12, 2007 Author Share Posted February 12, 2007 Ok, this is working great. I have it pulling in the newest title, and it links to the webpage that I want it to. But I'm still getting those messages at the top and bottom of the webpage. Table 'CTW_BM_db.client_module_customization' doesn't exist SELECT search, `replace` FROM client_module_customization WHERE client_module=50 AND active=1 And these error message at the bottom of my webpage: Table 'CTW_BM_db.page_view' doesn't exist SELECT ID FROM page_view WHERE month='2007-02' AND file_name='index' AND publication=5 AND client=1 AND source='module' Table 'CTW_BM_db.page_view' doesn't exist INSERT INTO page_view (month, file_name, publication, client, source, count) VALUES ('2007-02', 'index', 5, 1, 'module', 1) I don't understand why they would show up. Nowhere in my code am I looking for Table 'CTW_BM_db.client_module_customization' or the others it's referring to. Does anybody have any idea why the code I put in above would cause these messages to appear? Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 12, 2007 Share Posted February 12, 2007 Find the code that is trying to access those tables. Quote Link to comment Share on other sites More sharing options...
sydewyndr Posted February 12, 2007 Author Share Posted February 12, 2007 OK, I think I know what's causing the messages, but I'm not sure how to stop it. The rest of the webpage uses a separate file called "db.php" to pull in all kinds of other data from 1 or more MySQL databases. So the code I put in to pull this data in is conflicting with that, and trying to pull in tables that exist in the other database. Is there a way I can have this code execute and connect to the database called "CTW_BM_db" but then disconnect so it doesn't try to use that code on the rest of the page? Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 12, 2007 Share Posted February 12, 2007 mysql_select_db()/ Quote Link to comment Share on other sites More sharing options...
sydewyndr Posted February 12, 2007 Author Share Posted February 12, 2007 I'm not really sure where you meant for me to put that line of code, so I put it at the end. So now my code looks like this: <?php $host='localhost'; $username='admin'; $pass='n3wsyst3m'; $database='CTW_BM_db'; mysql_connect ($host, $username, $pass); mysql_select_db($database) or die('Could not connect to the Database'); $info = mysql_query("SELECT post_title FROM wp_posts ORDER BY ID DESC LIMIT 1"); while ($r = mysql_fetch_array($info)) { echo "</a href=http://contentthatworks.com/hs_blog/wordpress target=_blank>$r[post_title]<//a>"; } mysql_select_db(); ?> But I'm still getting the same messages at the top and bottom of my screen, and now I'm getting a new warning that shows up right below my output. The output is correct and exactly what I want it to be by the way. Warning: Wrong parameter count for mysql_select_db() Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 12, 2007 Share Posted February 12, 2007 Did you look up the function in the manual before using it? mysql_select_db($database) or die('Could not connect to the Database'); You want to copy that only make it the other database, the one that has the tables the code is looking for. 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.