PHP - 使用字段名称作为变量

时间:2013-08-28 20:49:34

标签: php mysql arrays variables record

我收到错误“警告:mysql_field_name()期望参数1是资源,对象在28号线上给出”

我对PHP很新,但我想要完成的是读取一个HTML文件并用记录的值替换自定义标记。标签结构是| + FIELD-NAME-Record#+ |例如,如果我的sql返回“Name”字段的两条记录,它将查找以下两个标记| + Name1 + |和| + Name2 + |在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 “+ |”,$行[$ I],$内容);

2 个答案:

答案 0 :(得分: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()将数据作为关联数组拉出,字段名称作为索引键。

答案 1 :(得分: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;
}