26692

mysql_fetch_array() giving error

Question:

The following code is giving error:

<blockquote>

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

</blockquote> <?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>

Answer1:

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.)

Answer2:

A couple things to try:

<ol><li>

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.

</li> <li>

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.

</li> </ol>

Answer3:

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); } }

Recommend

  • how to connect amazon rds mysql in iOS
  • How to store and retrieve text data in MySQL preserving the line breaks?
  • Can't get json out of mysql varchar but int
  • where is the fatal error “Cannot access empty property” in the PHP class function?
  • Get the ID of the selected check box
  • MySQL Update with PHP variables in a loop
  • Avoiding duplicated data in PostgreSQL database in Python
  • Uncaught TypeError: Object [object Object] has no method 'fancybox'
  • PreparedStatement - how specify to use default value of column
  • Inserting a duplicate record using Npgsql
  • literal does not match format string error on updating sql table
  • is it secure to write mysql_connect ( “localhost”, “root”, “mypasswd” ) on file?
  • Modifying the AJAX PHP database example
  • How to call mysqli_stmt with call_user_func_array?
  • In Jaspersoft Studio, Sql server procedure returning fewer fields than expected
  • Notice: Undefined index: image - unable to find the error
  • Error: String or binary data would be truncated. The statement has been terminated
  • how to check if a field is not unique
  • Can't connect using mysql_connect to Database in ipage.com hosting?
  • mongodb num_rows equivalent php
  • How to get the second value in a dropdown box based on the selected one in the first dropdown box
  • unable to obtain stable firefox connection in 60 seconds
  • Execute Success but num_rows return 0 [PHP-MySQL]
  • How to export MS Access table into a csv file in Python using e.g. pypyodbc
  • Displaying Data From Multiple MySQL Tables
  • Disable add to cart redirection
  • Passing parameter through “window.location.href”
  • JSON - slashes not escaping
  • Converting query results into DataFrame in python
  • PDO error when wrong host name
  • Randomizing -and remembering that randomisation- multiple choice questions in php
  • How can I extract results of aggregate queries in slick?
  • Needing to do .toArray() to get output of mongodb .find() on key name not value
  • MongoDB in PHP using aggregate to group by _id is null not working
  • Display Images one by one with next and previous functionality
  • ORA-29908: missing primary invocation for ancillary operator
  • How to get next/previous record number?
  • How do you join a server to an Active Directory (domain)?
  • Turn off referential integrity in Derby? is it possible?
  • How does Linux kernel interrupt the application?