cherubrock74 Posted May 16, 2009 Share Posted May 16, 2009 Can someone please check my code for errors? I keep getting the error message "database is down" here is the code I am using. <?php $hostname = "??????????????"; $username = "??????????????"; $password = "??????????????"; ?> <?php $dbh = mysql_connect($hostname, $username, $password) or die("Unable to connect to MySQL"); $selected = mysql_select_db("ajax_test",$dbh); if( isset($_POST['Submit']) ) { echo "<pre>"; print_r($_POST); } if( isset($_GET['ajax']) ) { //In this if statement switch($_GET['ID']) { case "LBox2": $query = sprintf("SELECT * FROM list2 WHERE List1Ref=%d",$_GET['ajax']); break; case "LBox3": $query = sprintf("SELECT * FROM list3 WHERE List2Ref=%d",$_GET['ajax']); break; } $result = mysql_query($query); echo "<option value=''></option>"; while ($row = mysql_fetch_assoc($result)) { echo "<option value='{$row['ID']}'>{$row['Name']}</option>\n"; } mysql_close($dbh); exit; //we're finished so exit.. } if (!$result = mysql_query("SELECT * FROM list1")) { echo "Database is down"; } //for use with my FIRST list box $List1 = ""; while ($row = mysql_fetch_assoc($result)) { $List1 .= "<option value='{$row['ID']}'>{$row['Name']}</option>\n"; } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Simple Dymanic Drop Down</title> <script language="javascript"> function ajaxFunction(ID, Param) { //link to the PHP file your getting the data from //var loaderphp = "register.php"; //i have link to this file var loaderphp = "<?php echo $_SERVER['PHP_SELF'] ?>"; //we don't need to change anymore of this script var xmlHttp; try { // Firefox, Opera 8.0+, Safari xmlHttp=new XMLHttpRequest(); }catch(e){ // Internet Explorer try { xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); }catch(e){ try { xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); }catch(e){ alert("Your browser does not support AJAX!"); return false; } } } xmlHttp.onreadystatechange=function() { if(xmlHttp.readyState==4) { //the line below reset the third list box incase list 1 is changed document.getElementById('LBox3').innerHTML = "<option value=''></option>"; //THIS SET THE DAT FROM THE PHP TO THE HTML document.getElementById(ID).innerHTML = xmlHttp.responseText; } } xmlHttp.open("GET", loaderphp+"?ID="+ID+"&ajax="+Param,true); xmlHttp.send(null); } </script> </head> <body> <!-- OK a basic form--> <form method="post" enctype="multipart/form-data" name="myForm" target="_self"> <table border="0"> <tr> <td> <!-- OK here we call the ajaxFuntion LBox2 refers to where the returned date will go and the this.value will be the value of the select option --> <select name="list1" id="LBox1" onchange="ajaxFunction('LBox2', this.value);"> <option value=''></option> <?php echo $List1; ?> </select> </td> <td> <select name="list2" id="LBox2" onchange="ajaxFunction('LBox3', this.value);"> <option value=''></option> <!-- OK the ID of this list box is LBox2 as refered to above --> </select> </td> <td> <select name="list3" id="LBox3"> <option value=''></option> <!-- OK the ID of this list box is LBox3 Same as above --> </select> </td> </tr> </table> <input type="submit" name="Submit" value="Submit" /> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/158401-database-is-down-error/ Share on other sites More sharing options...
wildteen88 Posted May 16, 2009 Share Posted May 16, 2009 Change if (!$result = mysql_query("SELECT * FROM list1")) { echo "Database is down"; } to if (!$result = mysql_query("SELECT * FROM list1")) { echo "Database is down<br />".mysql_error(); } Quote Link to comment https://forums.phpfreaks.com/topic/158401-database-is-down-error/#findComment-835384 Share on other sites More sharing options...
Mark Baker Posted May 16, 2009 Share Posted May 16, 2009 Close database Then execute a query against that database And you're surprised that the query doesn't return any results???? Quote Link to comment https://forums.phpfreaks.com/topic/158401-database-is-down-error/#findComment-835385 Share on other sites More sharing options...
cherubrock74 Posted May 16, 2009 Author Share Posted May 16, 2009 Hi thaks to both of you for the reply after changing my code by adding the line you suggested I get this error message: Database is down No database selected can you help me so my code will work? Mark please bear with me...I'm a begginner Quote Link to comment https://forums.phpfreaks.com/topic/158401-database-is-down-error/#findComment-835405 Share on other sites More sharing options...
cherubrock74 Posted May 16, 2009 Author Share Posted May 16, 2009 Ok I found the problem...I was missing my DB name in my code...so it was not "selected" I changed this line $selected = mysql_select_db("ajax_test",$dbh); with this line: $selected = mysql_select_db("my_database_name_here",$dbh); Now I am able to populate my first drop down menu, but nothing happens to the other two when I select something in the first... can someone help me troubleshot my code? Quote Link to comment https://forums.phpfreaks.com/topic/158401-database-is-down-error/#findComment-835482 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.