Jump to content

best way to check a table for info


harkly

Recommended Posts

What is the best way to go about checking a table to see if any information has been added (for a particular user)

 

my table:

 

music

userID | band | barbershop | classical | country | and about 15 more

 

 

My goal is to verify if any data for a particular user has been added to the table and if so, echo "Music I listen to: Band, etc"

if not then I do not want the "Music I listen to:" to echo at all.

 

I sort of have it working but am looking for the most efficient way to check the table.

 

$sql = mysql_query("SELECT * FROM music WHERE userId = 'test'");
$results = mysql_fetch_array( $sql );


  if ($results == null)
    { 
      echo " Blank \n";  
    }

  else
    {
      echo "  <br><br>Music I listen to:
              <span class='text'>
      \n";

      $sql = mysql_query("SELECT * FROM music WHERE userId = 'kelly'");

      while($r = mysql_fetch_array($sql)) {

        $string = "";

          if ($r['band']) $string .= "Band, ";
          if ($r['barbershop']) $string .= "Barbershop, ";
          if ($r['classical']) $string .= "Classical, ";
          if ($r['country']) $string .= "Country, ";
          if ($r['dance']) $string .= "Dance, ";
          if ($r['electric']) $string .= "Electroinical, ";
          if ($r['folk']) $string .= "Folk, ";
          if ($r['jazz']) $string .= "Jazz, ";
          if ($r['metal']) $string .= "Metal, ";
          if ($r['opera']) $string .= "Opera/Musicals, ";
          if ($r['orchestra']) $string .= "Orchestra, ";
          if ($r['punk']) $string .= "Punk Rock, ";
          if ($r['ragtime']) $string .= "Ragtime, ";
          if ($r['rap']) $string .= "Rap/Hip Hop, ";
          if ($r['reggae']) $string .= "Reggae/Ska, ";
          if ($r['religious']) $string .= "Religious, ";
          if ($r['rock_pop']) $string .= "Rock/Pop/Alternative, ";
          if ($r['world']) $string .= "World, ";
          if ($r['othr_music']) $string .= $r['othr_music_txt'];

          $string = rtrim($string, ", ");

          echo " $string \n";
        }
    }

 

 

I can get the results I want if I use something like this

  if ($band == null)

but I need to check at least 20 different fields.

Link to comment
Share on other sites

I don't think you can do what you are asking with your current structure. I'm not sure I even understand what the current structure is as it doesn't make sense to me. I would expect users may have multiple favorites under one genre (e.g. country) but non under another genre (e.g. barbershop).

 

You should modify your database structure. I would suggest the following:

 

1) a user table with basic user info, name, age, etc,

2. A genre with genre_id and genre_name. You would have one record for each genre you want to track

3. Finally a user_genre table to link the two above. The table could have fields for user_id, genre_id, artist_name, date_added.

 

You could then grab the most curretly added artist record for a user or know if there are none.

Link to comment
Share on other sites

My database is not what I am inquiring about. What you are proposing would not work for my site.

 

I am just wondering if there is a better way to go about checking a table to see if any data has been put into it, the options are NULL or 1. If there is an 1 in any of the fields do this....

 

I am not that familiar with all the php options to use and couldn't find anything in google that was close to what I am looking for, probably becuase I am not using the correct verbage.

 

Any ways this is what I have working.

 

  if ($band == null && $barbershop == null && $classical == null && $country == null && $dance == null && $electric == null && $folk == null && $jazz == null && $metal == null && $opera == null && $orchestra == null && $punk == null && $ragtime == null && $rap == null && $reggae == null && $religious == null && $rock_pop == null && $world == null && $othr_music == null)

 

 

is there not something like a function or an array I can set up that does the same thing.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.