mysql_fetch_array() problem
- by Marty
So I have 3 DB tables that are all identical in every way (data is different) except the name of the table. I did this so I could use one piece of code with a switch like so:
function disp_bestof($atts) {
extract(shortcode_atts(array(
'topic' => ''
), $atts));
$connect = mysql_connect("localhost","foo","bar");
if (!$connect) { die('Could not connect: ' . mysql_error()); }
switch ($topic) {
case "attorneys":
$bestof_query = "SELECT * FROM attorneys p JOIN (awards a, categories c, awardLevels l) ON (a.id = p.id AND c.id = a.category AND l.id = a.level) ORDER BY a.category, a.level ASC";
$category_query = "SELECT * FROM categories";
$db = mysql_select_db('roanoke_BestOf_TopAttorneys');
$query = mysql_query($bestof_query);
$categoryQuery = mysql_query($category_query);
break;
case "physicians":
$bestof_query = "SELECT * FROM physicians p JOIN (awards a, categories c, awardLevels l) ON (a.id = p.id AND c.id = a.category AND l.id = a.level) ORDER BY a.category, a.level ASC";
$category_query = "SELECT * FROM categories";
$db = mysql_select_db('roanoke_BestOf_TopDocs');
$query = mysql_query($bestof_query);
$categoryQuery = mysql_query($category_query);
break;
case "dining":
$bestof_query = "SELECT * FROM restaurants p JOIN (awards a, categories c, awardLevels l) ON (a.id = p.id AND c.id = a.category AND l.id = a.level) ORDER BY a.category, a.level ASC";
$category_query = "SELECT * FROM categories";
$db = mysql_select_db('roanoke_BestOf_DiningAwards');
$query = mysql_query($bestof_query);
$categoryQuery = mysql_query($category_query);
break;
default:
$bestof_query = "switch on $best did not match required case(s)";
break;
}
$category = '';
while( $result = mysql_fetch_array($query) ) {
if( $result['category'] != $category ) {
$category = $result['category'];
//echo "<div class\"category\">";
$bestof_content .= "<h2>".$category."</h2>\n";
//echo "<ul>";
Now, this whole thing works PERFECT for the first two cases, but the third one "dining" breaks with this error:
Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource ... on line 78
Line 78 is the while() at the bottom. I have checked and double checked and can't figure what the problem is. Here's the DB structure for 'restaurants':
CREATE TABLE `restaurants` (
`id` int(10) NOT NULL auto_increment,
`restaurant` varchar(255) default NULL,
`address1` varchar(255) default NULL,
`address2` varchar(255) default NULL,
`city` varchar(255) default NULL,
`state` varchar(255) default NULL,
`zip` double default NULL,
`phone` double default NULL,
`URI` varchar(255) default NULL,
`neighborhood` varchar(255) default NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=249 DEFAULT CHARSET=utf8
Does anyone see what I'm doing wrong here? I'm passing "dining" to the function and as I said before, the first two cases in the switch work fine.
I'm sure it's something stupid...