【发布时间】:2013-08-28 20:49:33
【问题描述】:
我收到错误“警告:mysql_field_name() 期望参数 1 是资源,第 28 行的...中给出的对象”
我对 PHP 还很陌生,但我想要完成的是读取 HTML 文件并用记录的值替换自定义标记。标签结构为|+FIELD-NAME-Record#+|例如,如果我的 sql 为“名称”字段返回两条记录,它将查找以下两个标签 |+Name1+|和|+姓名2+|在 HTML 文件中并将其替换为两个返回值。说亚当和吉姆。
下面是我到目前为止的代码。
$c = '|+Name1+|<br>|+Name2+|';
echo(ReplaceHTMLVariables(mysqli_query($con,"SELECT * FROM nc_names"),$c));
function ReplaceHTMLVariables($results, $content){
while($row = mysqli_fetch_array($results)){
//Define a variable to count what record we are on
$rNum = 1;
//Loop through all fields in the record
$field = count( $row );
for ( $i = 0; $i < $field; $i++ ) {
//Use the field name and record number to create variables to look for then replace with the current record value
$content = str_replace("|+".mysql_field_name( $results, $i ).$rNum."+|",$row[$i],$content);
}
//move to next record
$rNum++;
}
return $content;
}
第 28 行引用了这一行
$content = str_replace("|+".mysql_field_name($results, $i ).$rNum."+|",$row[$i],$content);
【问题讨论】:
-
你不能混合使用 mysql 和 mysqli 扩展。
-
总是检查mysqli_query() 的返回值,如果有错误将是FALSE。不要只是假设它会返回结果。
标签: php mysql arrays variables record