-1

I am trying to create an HTML list for every element in a MySQL table, but I'm returning an error and I'm not sure how to fix it. It says "Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/rmbennin/htdocs/labs/dataDesign/dbLib.php on line 214"

The code here is calling the list:

 global $connect;
 $output = "";
 $output .= '<form method="post" action="sendData.php">';

 //process a query just to get field names
 $query = "SELECT * FROM $tableName";
 $result = mysql_query($query, $connect) or die(mysql_error());

if($tableName == "meals"){
    $theField = mysql_fetch_field($result);
    $fieldName = $theField->name;
    $Name = fieldToList($tableName, "Name", "foodsID", $fieldName);

    $output .= <<< HERE

    * Name of meal:<br>
    $Name<br>

    * How many servings did you have?:<br>
    <input type="number" name="Servings" required><br>

HERE;

The code the error is coming from is this:

function fieldToList($tableName, $keyName, $keyVal, $fieldName){

 global $connect;
 $output = "";
 $query = "SELECT $keyName, $fieldName FROM $tableName";
 $result = mysql_query($query, $connect);
 $output .= "<select name= \"$keyName\">\n";
 $recNum = 1;
 while ($row = mysql_fetch_assoc($result)){
   $theIndex = $row["$keyName"];
   $theValue = $row["$fieldName"];
   $output .= <<< HERE
   <option value = "$theIndex"
HERE;

if ($theIndex == $keyVal){
  $output .= " selected = \"selected\"";
} // end if
$output .= ">$theValue</option>\n";
$recNum++;
} // end while
$output .= "</select>\n";
return $output;
} // end fieldToList
2
  • Your query is failing; you've not got any error checking in there, so you're assuming it always works - you should add in a call to mysql_error() if it doesn't. You should also look at moving to PDO or mysqli_, too, as the mysql_ functions are being deprecated. Commented Jun 14, 2013 at 16:59
  • seems a legit question to me, don't understand the downvotes... Commented Jun 14, 2013 at 17:12

3 Answers 3

0

Try to put cell names and table names inside of backticks.

For example:

$query = "SELECT * FROM $tableName";

becomes

$query = "SELECT * FROM `$tableName`";

and also

$query = "SELECT `$keyName`, `$fieldName` FROM `$tableName`";

Other possible solution: does global $connect; also selects the database?


Other possible solution: try to var_dump($fieldName) and the other variables you're passing in the query. Do they correspond to what you have in the db?


Also, you should stop using mysql_* functions. They're deprecated and a potential hole for the security of your system. Instead, switch to PDO or Mysqli. This article might help you decide which one to choose.

Sign up to request clarification or add additional context in comments.

3 Comments

Didn't work. I believe the problem is I'm calling the wrong things in fielToList, but I'm not sure what I should call.
It's just practice, it's not for a commercial site. Don't worry, I know I shouldn't use the mysql_* functions!
have you backticked ALL of your queries? See updated answer, try to copypaste it.
0

According to me there is some error in your query. change following part of code

 while ($row = mysql_fetch_assoc($result)){

To

 if($result)
 {
   while ($row = mysql_fetch_assoc($result)){

this will check whether query results argument is 1 or 0, if query will executed correctly then this part will work otherwise this will not. And over all you will not get any error.

Comments

0

add some debuging code.

echo $query; //to make sure its correct. 


 if (!$connect)
   {
   die('could not connect: ' . mysql_error());
   }

$query = "SELECT $keyName, $fieldName FROM $tableName";
$result = mysql_query($query,$connect)  or die(mysql_error());
print_r(mysql_fetch_assoc($result));

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.