mattwal Posted April 2, 2009 Share Posted April 2, 2009 Hello everyone, What I am trying to do is check the database in the "posts" table to see which blog posts are set to published. If it has a "Y" in the published field then display the post on the page. If it has a "N" it should not display the post on the page. The problem is i have no clue how to go about this because im learning from a book and it goes something like this: <?php //DO THE DATABASE CONNECTION $conn = mysql_connect('localhost','DB_User','DB_Pass') or trigger_error("SQL", E_USER_ERROR); $db = mysql_select_db('DB_Name',$conn) or trigger_error("SQL", E_USER_ERROR); // select all fields in posts and format the date to jan 1st, 2002 type $link1 = "SELECT post_id, author, section, category, title, post, publish, DATE_FORMAT(date, '%b %D, Y%') AS date FROM posts DESC"; $result = @msql_query($link1); if ($result) { // If it ran OK, Display the records // Fetch and print all the records. while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { echo '//print the returned recoreds here '; } mysql_free_result ($result); // Free up the resources. } else { // If it did not run OK. echo '<p class="error">There are currently no registered users.</p>'; } ?> The published field is a CHAR(2) with a default setting to N. If anyone is able to point me in the right direction on how to accomplish this I would be realy appreciative! Link to comment https://forums.phpfreaks.com/topic/152158-checking-db-if-post-is-published/ Share on other sites More sharing options...
mattwal Posted April 2, 2009 Author Share Posted April 2, 2009 Sorry i didn't see an edit button for my previous post. Here what I have so far and it's giving me a parse error on line 41 which is the last else statment. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> </head> <body> <h2>Testing "is publish" Feature</h2> <?php /**** Dealing with the database ****/ // connect to db $conn = mysql_connect('localhost','DB_User','DB_Pass') or trigger_error("SQL", E_USER_ERROR); $db = mysql_select_db('DB_Table',$conn) or trigger_error("SQL", E_USER_ERROR); // select all fields in posts and format the date to jan 1st, 2002 type $link1 = "SELECT post_id, author, section, category, title, post, publish, DATE_FORMAT(date, '%b %D, Y%') AS date FROM posts DESC"; $result = @msql_query($link1); if ($result) { // If it ran OK, Display the records while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { //checking if there are any approved posts $pub = $row['publish']; if ($pub == 'Y') { //display the results echo ' ' . $row['author'] . '<br /> ' . $row['post'] . '<br /> ' . $row['date'] . '<br />'; } else { echo '<p>Sorry there are published posts yet...</p>'; } // end of $pub check mysql_free_result($result); } else { // If it didn't run O.K. echo '<p>' . msql_error() .'<br />Query: ' . $link1. '</p>'; } //end 1st if ?> </body> </html> I am wondering if I'm heading ing the right direction or not? Link to comment https://forums.phpfreaks.com/topic/152158-checking-db-if-post-is-published/#findComment-799191 Share on other sites More sharing options...
Showcase Posted April 2, 2009 Share Posted April 2, 2009 You're headed in the right direction. However, while is a loop, and even if there's a published post somewhere in the sql return, it will display "There are no published posts" in between the displayed info for the published posts whenever the loop encounters an unpublished post. You could achieve the right results by modifying your code in the if statement, or you can simply add " WHERE publish='Y' " to the end of your mysql query. With this query, it will only return the rows in which publish is Y. Link to comment https://forums.phpfreaks.com/topic/152158-checking-db-if-post-is-published/#findComment-799205 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.