当前位置: 首页 > 知识库问答 >
问题:

PHP-使用字段名称作为变量[重复]

邓嘉致
2023-03-14

我得到的错误"警告:mysql_field_name()期望参数1是资源,对象在...在第28行"

我对PHP相当陌生,但我试图完成的是读取超文本标记语言文件,并用记录的值替换自定义标记。标签结构是|FIELD-NAME-Recage#|例如,如果我的sql返回两个记录的"名称"字段,它将在超文本标记语言文件中查找以下两个标签|Name1|和|Name2|,并用返回的两个替换它价值观。说亚当和吉姆。

下面是我到目前为止的代码。

$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)$纳姆。“|”,$行[$i],$内容);

共有2个答案

闻人高卓
2023-03-14

@Barmar的评论是正确的,您不能混合使用mysql_u和mysqli_u函数,这就是为什么您要声明错误的原因

我还对你的代码做了一些其他的修改来简化它。有关解释,请参阅内联注释

$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;
}
堵景天
2023-03-14

你正在混合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()将数据作为关联数组,字段名作为索引键。

 类似资料:
  • 当我尝试这个代码时,我不能给对象中的“option\u code”一个常量。 我想要这样的结果(选项代码需要更改)= 这可能吗?

  • 问题内容: 我想使用变量的值来访问对象。 假设我有一个名为myobject的对象。 我想用此名称填充变量,然后使用该变量访问对象。 例: 问题答案: 全球: 全球:v2 本地:v1 本地:v2

  • 问题内容: 我在python中有此代码。 我将从这样的命令行运行此脚本 因此,我需要一种使len使用列表而不是变量的方法,我只想在var中给出列表的名称。 我知道在这个例子中我可以做 但是我的脚本上有13个列表,我需要一种方法,可以通过在命令行上输入的名称来引用它们。 问题答案: 尽管这在Python中是完全可能的,但不是必需的。最好的解决方案是不执行此操作,而是使用字典。 您会注意到脚本中有13

  • 问题内容: 我们可以通过定义模板名称,然后通过将其加载到其他(父)模板中。 如何通过变量value加载模板。还是不可能? 问题答案: 不幸的是你不能。 该动作的语法: 要包含的模板的名称是一个 常量 字符串,它不是 管道 ,在执行过程中可能会根据参数而有所不同。 如果允许的语法为: 那么您可以使用类似的命令,但是由于语法仅允许使用常量字符串,因此您不能这样做。 从Rob推论为什么不允许动态模板调用

  • 问题内容: 我有一个分配了字符串的变量,我想根据该字符串定义一个新变量。 问题答案: 你可以使用exec:

  • 问题内容: 是否可以选择名称为字符串的字段? 我需要这个来让触发器具有像这样的动态字段名称 问题答案: 如果该字符串在您的外部应用程序(如PHP)中,请确保构造MySQL语句。 如果字符串在MySQL表中,则不能。MySQL没有或没有这样的功能。以下是不可能的: 假设您有一个表,其中的字段引用该表中的列名之一。可能会有其他列允许您选择所需的列。 但是,您可以使用PREPARED STATEMENT