0

How can I fetch from database and echo the title, desc, and image, listing all of only PUBLISHED blogs I have stored in the database onto a web page? I would like to list them all on the web page, sort of like Dreamweaver's repeat region.

I have a table field in the database called Publish. User has to select Yes or No. I only want to display published blogs set to YES on the web page.

Thanks in advance.

  • You would need to use a [`WHERE`](http://www.mysqltutorial.org/mysql-where/) clause. – Funk Forty Niner May 26 '14 at 02:48
  • I'm sorry Fred, while you were answering my original post, I was making an edit to my question to be more specific as to what I was looking for. Thanks. – Soletwosole May 26 '14 at 02:52
  • You're welcome. My comment about the WHERE clause is pretty much what you can use in order to achieve this. Plus, you'd setup a submit button or a radio button with two choices, then based on the selection a person's chosen, you would use that based on a conditional statement. – Funk Forty Niner May 26 '14 at 02:55
  • Would it be like this: SELECT * FROM blogs ORDER BY id DESC WHERE published='Yes' – Soletwosole May 26 '14 at 03:00
  • Almost; place your ORDER BY id DESC after your WHERE clause. `SELECT * FROM blogs WHERE published='Yes' ORDER BY id DESC` – Funk Forty Niner May 26 '14 at 03:03
  • You're welcome. It'll work. If you have any problems, let me know. – Funk Forty Niner May 26 '14 at 03:09

1 Answers1

0

Use this as your query:

SELECT * from blogs WHERE blogs.Publish = 'Yes'

then you'd use a loop:

<?php
   while($row = mysqli_fetch_array($result)) {
      echo $row['title'] . ' ' . $row['desc'] . ' <img src="' . $row['image'] . '" />';
      echo "<br />";
   }
?>

Then just use that to add your own HTML and CSS around it.

More info: http://www.w3schools.com/php/php_mysql_select.asp

James Heazlewood
  • 862
  • 9
  • 12