-1

我收到错误“警告: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);

4

2 回答 2

1

您正在将 MySQL 与 MySQLi 混合使用,如果您使用 mysqli_fetch_assoc() 来代替,您实际上并不需要这样做:

function ReplaceHTMLVariables($results, $content)
{
    //Define a variable to count what record we are on
    $rNum = 1;

    /*** Using Mysqli::fetch_assoc() ***/
    while( $row = mysqli_fetch_assoc( $results ) )
    {
        //Loop through all fields in the record
        /*** Variable $value passed by reference for performance ***/
        foreach( $row as $fieldName => &$value ) {
            $content = str_replace("|+".$fieldName.$rNum."+|",$value,$content);
        }

        ++$rNum; /*** Faster than $rNum++ **/
    }
    return $content;
}

mysqli_fetch_assoc()将数据作为关联数组提取,字段名称作为索引键。

于 2013-08-28T21:11:03.327 回答
0

@Barmar 评论是正确的,您不能混合使用 mysql_ 和 mysqli_ 函数,这就是您收到错误消息的原因

我还对您的代码进行了一些其他更改以简化它。请参阅内联注释以获取解释

$c = '|+Name1+|<br>|+Name2+|';

// database query on separate line, not in function call, so we can catch any errors
$res = mysqli_query($con,"SELECT * FROM nc_names") or die(mysqli_error());

echo(ReplaceHTMLVariables($res,$c));


function ReplaceHTMLVariables($results, $content){

    //Define a variable to count what record we are on
    $rNum = 1;

    // use fetch_assoc instead of fetch_array
    // fetch_assoc will give you an array of column_name => value pairs
    while($row = mysqli_fetch_assoc($results)){

        // MOVED this outside the loop or you will reset to 1 it for each loop through
        //Define a variable to count what record we are on
        //$rNum = 1;

        // extract column names from associative array
        $field_names = array_keys($row);

        //Loop through all fields in the record

        // use foreach loop instead of for loop (this is just my preference, not essential)            // $field = count( $row );    
        //for ( $i = 0; $i < $field; $i++ ) {
        foreach ($field_names as $field_name) { 
            // Use the field name and record number to create variables to look for then replace with the current record value

            // build placeholder on separate line, makes code easier to read
            $placeholder = "|+{$field_name}{$rNum}+|"; 

            $content = str_replace($placeholder, $row[$field_name], $content);
        }

        // move to next record
        $rNum++;
    }

    return $content;
}
于 2013-08-28T21:13:32.333 回答