tags:

views:

32

answers:

3

The following code is giving error:

*Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in W:\Aptana WorkSpace\Website\lib\filldropdown.php on line 22*

<?php
error_reporting(E_ALL ^ E_NOTICE);

$db_host = 'localhost:3306';
$db_username = 'superuser';
$db_password = 'admin';
$db_name = 'exampledb';

//connect to the database server
$connection = mysql_connect($db_host, $db_username, $db_password) or die(mysql_error());
//select database
$db = mysql_select_db($db_name, $connection);

$dropdownControlName = $_GET['DropDownControlName'];
$divName = $_GET['DivName'];
$sqlQuery = $_GET['SqlQuery'];
$result = mysql_query($sqlQuery);
?>
<select name=" <?php $dropdownControlName ?> ">
<option>Select Category</option>
<?php while($row=mysql_fetch_array($result))
{ ?>
    <option value="<?= $row[0] ?>"><?= $row[1] ?></option>
<?php } ?>
</select>
+1  A: 

Generally that means something is wrong with your SQL. Try echoing out the sql to see if it looks correct.

(Also, passing a GET variable directly in as a query is a VERY bad idea in a security sense - you're just asking for some malicious user to totally abuse the opening and compromise your database.)

Amber
At present it is just for testing the dropdown. Will improve it.
RPK
+1  A: 

A couple things to try:

  1. Run the query inside of the mysql client, to be 100% sure it's correct. If it won't execute in the client, it won't execute via DBI.

  2. Print out the query before it's passed to DBI. Often you'll find that something is quoted strangely and needs to be escaped differently before passing it to MySQL.

Wade Williams
I am trying echo ($sqlQuery), but it is not showing anything on screen. Probably, it gets hidden behind the HTML Form.
RPK
A: 

Ok, I found the problem. The GET is not receiving any value. Below is the AJAX code that I am using to call the above PHP code file:

function MakeRequest(DivName, DropDownName, SqlQuery)
{
  var xmlHttp = getXMLHttp();

  var strUrl = "./lib/filldropdown.php?DivName = " + DivName + "&DropDownControlName = " + DropDownName + "&SqlQuery = " + SqlQuery;

  try 
  {
    xmlHttp.onreadystatechange = function()
    {
     if (xmlHttp.readyState == 4) 
     {
      HandleResponse(xmlHttp.responseText, DivName);
     }
    }
      xmlHttp.open("GET", strUrl, true);
      xmlHttp.send(null);
    }
    catch(err)
    {
     alert(err);
    }
}
RPK