Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/content/05/6932805/html/wp-content/plugins/ext-db-manage/db_manager_admin.php on line 87
line 87
while($ext_db_row = mysql_fetch_array($result) or die())
<?php
/*
Plugin Name: External Database Management
Plugin URI: http://wordpress.org/support/topic/222660
Description: To manage simple external mySQL database and insert into a wordpress page or post as a table. (See Readme for instructions.) (Updated for WP 2.7)
Version: 0.2
Author: Driftless1
Author URI: http://www.lbell270.com
*/
/* Copyright 2008/2009 Driftless1
This is designed to help with database issues.
*/
/*
IMPORTANT NOTE:
This framework assumes you have at least a basic grasp of mySQL.
This plugin will not create the table, but rather assumes you have
created the table already, and know how to connect to it.
The connection information goes into the db_manager_config.php file
To insert a table into your page or post, use <?php insert_ext_db_table() ?>
in a template (or in the post if you have PHP enabled.)
*/
// Hook for adding admin menu
add_action('admin_menu', 'ext_db_admin');
function ext_db_admin() {
add_menu_page('External Database', 'External DB', delete_others_pages, 'db_manager_admin.php', 'db_manage_page');
}
//this function is the back end - to put the DB in the Admin pages for easy access.
function db_manage_page() {
include("db_manager_config.php"); //defines connection properties - make sure you have the right info in the db_manager_config.php file
$con = mysql_connect(EXT_DB_HOST, EXT_DB_USER, EXT_DB_PASSWORD, "new");
if (!$con)
{
die('<br />Sorry, we can not connect to the database at this time. Please try to refresh.<br />If the problem persists, please contact the <a href="mailto:">administrator</a>. Thank you.<br /> ' . mysql_error());
}
mysql_set_charset(EXT_DB_CHARSET); // check this... may not work with earlier versions of PHP - I think only PHP 5.0 and above...
mysql_select_db(EXT_DB_NAME, $con);
echo "<div class='wrap'>";
echo "<h2>External Database Management Page</h2>";
if(!isset($_GET['ext_db_action'])&&(!isset($_POST['ext_db_action']))) //Show the table as default action
{
$result = mysql_query("Name, Phone_Number, Price, Paid, Brand, Item_Number, First_Date, Date_Ordered, Date_Received, Comments "); // ***EDIT THIS*** your custom mySQL statement to pull the data you want from your table
?>
<!-- this chunk of goodies adds an "add entry" button -->
<form action="<?php echo $PHP_SELF;?>" method="get">
<input type="hidden" name="page" value="db_manager_admin.php">
<input type="hidden" name="ext_db_action" value="edit">
<input type="hidden" name="entry" value="new">
<input class="button-primary" type="submit" value="Add Order">
</form><br />
<div id="ext_db_table_div">
<table id="ext_db_table" class="widefat" border="1">
<thead>
<tr>
<th scope="col" width="5%">Name</th> <!-- ***EDIT THESE*** Change the column headings to whatever you desire... -->
<th scope="col" width="5%">Phone Number</th>
<th scope="col" width="5%">Price</th>
<th scope="col" width="5%">Paid</th>
<th scope="col" width="5%">Brand</th>
<th scope="col" width="5%">Item #</th>
<th scope="col" width="5%">First Date</th>
<th scope="col" width="5%">Date Ordered</th>
<th scope="col" width="5%">Date Received</th>
<th scope="col" width="5%">Comments</th>
<!-- you get the point - add the columns you want here just be sure the columns below are the same number, or things look funky...-->
</tr>
</thead>
<tbody>
<?php
while($ext_db_row = mysql_fetch_array($result) or die()) //While there is data in the array, keep building the table. Below, you call each column with $ext_db_row['foobar'] where foobar is the heading for each column
{
// in $ext_db_row['db_id'], the 'db_id' should be changed to whatever the uniqe ID (primary key?) column of your mySQL table
echo "<td><strong><a href='admin.php?page=db_manager_admin.php&ext_db_action=edit&entry=" . $ext_db_row['db_id'] . "'>" . $ext_db_row['Name'] . "</a></strong></td>\n"; //***EDIT THIS*** Change 'db_id' to your uniqe key column, and 'column1' to whatever the column is called in your DB
if ($ext_db_row['Phone_Number'] != null) //all this if/else BS is a set of conditionals to display blank spaces if there is no data in a cell... dumb - sure there is a better way, but it works
echo "<td>" . $ext_db_row['Phone_Number'] . "</td>\n";
else echo "<br/> </td>\n";
if ($ext_db_row['Price'] != null)
echo "<td>" . $ext_db_row['Price'] . "</td>\n";
else echo "<br/> </td>\n";
if ($ext_db_row['Paid'] != null)
echo "<td>" . $ext_db_row['Paid'] . "</td>\n";
else echo "<br/> </td>\n";
if ($ext_db_row['Brand'] != null)
echo "<td>" . $ext_db_row['Brand'] . "</td>\n";
else echo "<br/> </td>\n";
if ($ext_db_row['Item_Number'] != null)
echo "<td>" . $ext_db_row['Item_Number'] . "</td>\n";
else echo "<br/> </td>\n";
if ($ext_db_row['First_Date'] != null)
echo "<td>" . $ext_db_row['First_Date'] . "</td>\n";
else echo "<br/> </td>\n";
if ($ext_db_row['Date_Ordered'] != null)
echo "<td>" . $ext_db_row['Date_Ordered'] . "</td>\n";
else echo "<br/> </td>\n";
if ($ext_db_row['Date_Received'] != null)
echo "<td>" . $ext_db_row['Date_Received'] . "</td>\n";
else echo "<br/> </td>\n";
if ($ext_db_row['Comments'] != null)
echo "<td>" . $ext_db_row['Comments'] . "</td>\n";
else echo "<br/> </td>\n";
if ($ext_db_row['Paid'] != null)
echo "<td><a href=\"http://" . $ext_db_row['Paid'] . "\" title=\"" . $ext_db_row['Paid'] . "\"><img src=\"" . $linkicon . "\" /></a></td>\n"; // these are just examples of using icons and links in a cell.
}//end while
mysql_close($con); //always good to close the connection
?>