How to Run PHP Code in WordPress

I was presented with a challenge the other day to retrieve data residing on a MySQL database and have it displayed on our client’s page. I decided to use PHP code since WordPress is built upon PHP but surprisingly without a plugin WordPress ignores the PHP code.

Also surprisingly was the fact that there are a lot of plugins to help with this problem. For my challenge I chose PHP-Exec, but I’ve seen others recommend PHP-Execution because it does NOT turn off the visual editor.

Below is the code that I used to connect to the mySQL database, create a table (I know, I know no DIV or CSS?) and set up an array to have the data run through and populate the table in the table rows.

<?php
//Connect To Database
$hostname=’xxxx’;
$username=’xxxx’;
$password=’xxxx’;
$dbname=’xxxx’;$usertable=’xxxx’;
mysql_connect($hostname,$username, $password) OR DIE (‘Unable to connect to database! Please try again later.’);mysql_select_db($dbname);

echo ”
<table border=’1′ width=’500′><tr><th>First Name</th><th>Last Name</th><th>Email</th></tr>”;
$query = ‘SELECT * FROM ‘ . $usertable;
$result = mysql_query($query);if($result) {    while($row = mysql_fetch_array($result)){       echo “<tr width=’500′>”;  echo “<td>” . $row['FirstName'] . “</td>”;  echo “<td>” . $row['LastName'] . “</td>”;
echo “<td><a href=\”mailto:” . $row['Email']. “\”>” . $row['Email']. “</a></td>”;
echo “</tr>”;
}
echo “</table>”;
}
?>

Here is the video tutorial on YouTube:

Post comment as twitter logo facebook logo
Sort: Newest | Oldest

Hi Ganesh,
Did you go to your profile and under PHP-Exec did you turn off the WYSIWYG editor option? If you don't your PHP code won't work on the page or post.

Used this code and follow your video tutorial.but it is not working. i get same result in preview page(display the full php code). please give me another idea