Beginner: Trouble retrieving data from MySQL

Hi guys

I’m just starting out in PHP. I would like to retrieve some data from the database and display it on my webpage

This is my code so far but obviously it does not work…

any ideas on how to correct this?

[php]

<?php //connection variables $host = "localhost"; $usernamehost = "admin"; $passwordhosts = "admin"; $db_name = "wordpress"; //connection to the db_name mysql_connect("$host", "$usernamehost", "$passwordhosts") or die ("cannot connect"); //select the db_name mysql_select_db("$db_name") or die ("cannot select DB"); ?> <?php $sql = "SELECT post_title, post_excerpt FROM wp_posts WHERE post_status = 'publish' AND post_type = 'post' ORDER BY post_date_gmt DESC LIMIT 0, 9" while ($data = mysql_fetch_array($sql)) { ?>

<?php echo $data["post_title"]; ?>

<?php echo $data["post_excerpt"]; ?>

<?php endforeach; ?>

[/php]

The way to correct it is to stop using obsolete Mysql code. Download the PDO bumpstart database in my signature. It will get you going in the right direction.

I agree with Kevin.

I don’t know where you go the code, but it is a mismatch.

A while loop does not end with a specialty endforeach. Did you attempt to modify a WordPress theme?

Sponsor our Newsletter | Privacy Policy | Terms of Service