Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 655897
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T22:39:19+00:00 2026-05-13T22:39:19+00:00

So I have 3 DB tables that are all identical in every way (data

  • 0

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…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T22:39:19+00:00Added an answer on May 13, 2026 at 10:39 pm

    You should always initialize the variable you use to some (null) value and then check for it before using it. My guess is that your third case (dining) never gets executed because of some misspelled identifier or something. This causes default: to run, after which your while() will execute anyway. However, $query is not set to anything useful.

    Therefore, you should throw an exception or otherwise break execution in the default: handler. Or, you may initialize $query = null; before the switch() and only do the while() loop when $query !== null.


    On a related note: you might code more efficient when you instead use the following (note the exception handler):

    $db_name = null;
    $table = null;
    switch ($topic) {
      case "attorneys":
        $db_name = 'roanoke_BestOf_TopAttorneys';
        $table = 'attorneys'
      break;
      case "physicians":
       $db_name = 'roanoke_BestOf_TopDocs';
       $table = 'physicians'
      break;
      case "dining":
       $db_name = 'roanoke_BestOf_DiningAwards';
       $table = 'restaurants'
      break;
      default:
       throw new Exception("Unknown topic.");
      break;
     }
    
    $bestof_query = "SELECT * FROM $table 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($db_name);
    $query = mysql_query($bestof_query);
    $categoryQuery = mysql_query($category_query);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Pages table that stores all my view urls and this table
I have a table (participants) which has multiple columns that could all be distinct.
I have a wordpress loop that pulls all of my data from a table...
Say I have a database that stores table data by year using an identical
I have 3 similar tables, that would all have the same enum type field.
I have a User table that has all of their avatars saved in an
I have a MySQL table & fields that are all set to UTF-8. The
I have some tables that benefit from many-to-many tables. For example the team table.
I have a database with 500+ tables, each with identical structure, that contain historical
I have three UITableViews that should look identical. Each is placed inside a different

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.