【问题标题】:PHP - Use Field Names as Variable [duplicate]PHP - 使用字段名称作为变量
【发布时间】: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


【解决方案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() 以关联数组的形式拉取数据,以字段名作为索引键。

【讨论】:

  • 你错过了 $rNum = 1;在循环内部,但在 foreach 方面做得比我好。您是否有任何关于 ++$rNum 更快(只是出于兴趣)的参考资料
  • @bumperbox 谢谢,我错过了那个,把它移了上去。个人测试以及几篇文章表明++$x$x++ 快​​。我基本上通过运行for( $i = 0; $i &lt; 1000000; $i++ ) { $a = $a + $i } 来检查它。在我的服务器上,$i++ 的时间为 0.85-0.95 秒,++$i 的平均时间约为 0.6-0.7。 (除非你增加几十万次,否则可能不会真正影响你的脚本)。
  • 谢谢你们太棒了!!! @bumperbox 感谢您的内联解释。这真的帮助我理解了。
【解决方案2】:

@Barmar 评论是正确的,你不能混合使用 mysql_ 和 mysqli_ 函数,这就是你得到错误声明的原因

我还对您的代码进行了一些其他更改以简化它。解释见内联 cmets

$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;
}

【讨论】:

  • 我在尝试将其实现到 wordpress 插件中时遇到此代码的问题。我正在使用 $result = $wpdb->get_results($sql);返回我的结果,但我认为它将它作为数组返回,因为我收到以下错误。 “警告:mysqli_fetch_assoc() 期望参数 1 为 mysqli_result,数组中给出...” 有没有办法将 $wpdb->get_results 返回的数组转换为该函数可以读取的记录集?
  • 你能做一个 var_dump($result) 来确保它实际上是返回一个数组吗?
猜你喜欢
  • 1970-01-01
  • 2011-06-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多