Jump to content

[SOLVED] Help with MadTechie's Dynamic DropDown PHP/AJAX code


Recommended Posts

When i run the code i get:

Database is down

and

Warning: mysql_fetch_assoc(): on line 34

 

My tabel:

`RayMusicCategories` (`ID`, `Parent`, `Title`) VALUES
(1, 0, 'House'),
(2, 1, 'Progressive House'),
(3, 1, 'Acid house'),
(4, 1, 'Deep house'),
(5, 1, 'Electro house'),
(6, 1, 'Tech house'),
(7, 1, 'Tribal house'),
(8, 0, 'Trance'),
(9, 2, 'Acid trance'),
(10, 2, 'Hard trance'),
(11, 2, 'Progressive trance'),
(12, 2, 'Vocal trance');

 

Here is the code:

<?php
   $hostname = "localhost";
   $username = "";
   $password = "";
   $dbh = mysql_connect($hostname, $username, $password)
      or die("Unable to connect to MySQL");
   $selected = mysql_select_db("RayMusicCategories",$dbh);
      if( isset($_POST['Submit']) )
      {
         echo "<pre>";
         print_r($_POST);
      }
      if( isset($_GET['ajax']) )
   {
      //In this if statement
      $query = sprintf("SELECT * FROM RayMusicCategories WHERE PARENT=%d",$_GET['ajax']);
      
      $result = mysql_query($query);
      echo "<option value=''></option>";
      while ($row = mysql_fetch_assoc($result))
      {
         echo "<option value='{$row['ID']}'>{$row['TITLE']}</option>\n";
      }
      mysql_close($dbh);
      exit; //we're finished so exit..
   }
   
   if (!$result = mysql_query("SELECT * FROM RayMusicCategories WHERE PARENT=0"))
   {
      echo "Database is down";
   }
   //for use with my FIRST list box
   $List1 = "";
   while ($row = mysql_fetch_assoc($result))
   {
      $List1 .= "<option value='{$row['ID']}'>{$row['TITLE']}</option>\n";
   }
?>

 

For some reason this looks wrong to me.

<?php
   if (!$result = mysql_query("SELECT * FROM RayMusicCategories WHERE PARENT=0"))
   {
      echo "Database is down";
   }
?>

 

-Lars

 

 

Change:

if (!$result = mysql_query("SELECT * FROM RayMusicCategories WHERE PARENT=0"))
   {
      echo "Database is down";
   }

to:

$result = mysql_query("SELECT * FROM RayMusicCategories WHERE PARENT=0") or die(mysql_error());

 

I replaced code with both suggestions and get No database selected error

 

<?php
   $hostname = "localhost";
   $username = "";
   $password = "";
   $dbh = mysql_connect($hostname, $username, $password)
      or die("Unable to connect to MySQL");
   $selected = mysql_select_db("RayMusicCategories",$dbh);
      if( isset($_POST['Submit']) )
      {
         echo "<pre>";
         print_r($_POST);
      }
      if( isset($_GET['ajax']) )
   {
      //In this if statement
      $query = sprintf("SELECT * FROM RayMusicCategories WHERE Parent=%d",$_GET['ajax']);
      
      $result = mysql_query($query);
      echo "<option value=''></option>";
      while ($row = mysql_fetch_assoc($result))
      {
         echo "<option value='{$row['ID']}'>{$row['TITLE']}</option>\n";
      }
      mysql_close($dbh);
      exit; //we're finished so exit..
   }
   
$result = mysql_query("SELECT * FROM RayMusicCategories WHERE Parent=0") or die(mysql_error());
   //for use with my FIRST list box
   $List1 = "";
   while ($row = mysql_fetch_assoc($result))
   {
      $List1 .= "<option value='{$row['ID']}'>{$row['TITLE']}</option>\n";
   }
?>

 

My connection properties are correct and server is up.

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.