我正在尝试为 MySQL 表中的每个元素创建一个 HTML 列表,但我返回一个错误,我不知道如何修复它。它说“警告:mysql_fetch_assoc():提供的参数不是第 214 行 /home/rmbennin/htdocs/labs/dataDesign/dbLib.php 中的有效 MySQL 结果资源”
这里的代码调用列表:
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;
错误来自的代码是这样的:
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