-1

I am getting the error "Warning: mysql_field_name() expects parameter 1 to be resource, object given in... on line 28"

I am fairly new to PHP, but what I am trying to accomplish is read a HTML file and replace a custom tag with the value of the record. The tag structure is |+FIELD-NAME-Record#+| For example if my sql returns two records for the "Name" field it will look for the following two tags |+Name1+| and |+Name2+| in the HTML file and replace it with the two returned values. Say Adam and Jim.

Below is the code that I have so far.

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

Line 28 references this line

$content = str_replace("|+".mysql_field_name( $results, $i ).$rNum."+|",$row[$i],$content);

2 Answers2

1

You're mixing MySQL with MySQLi, and you don't really need to do all that if you pull mysqli_fetch_assoc() instead:

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() Pulls the data as an associative array, with the field name as the index key.

Jason
  • 1,987
  • 1
  • 14
  • 16
  • 1
    You missed $rNum = 1; inside the loop, but did a better job of the foreach then me. Do you have any references regarding ++$rNum being faster (just out of interest) – bumperbox Aug 28 '13 at 21:18
  • @bumperbox Thanks, I missed that one, moved it up. Personal tests as well as several articles have shown that `++$x` is faster than `$x++`. I checked it by basically running `for( $i = 0; $i < 1000000; $i++ ) { $a = $a + $i }`. On my server, `$i++` had a time of 0.85-0.95 seconds, `++$i` got around 0.6-0.7 on average. (Probably won't really affect your script much unless you're incrementing several hundred thousand times). – Jason Aug 28 '13 at 21:22
  • Thanks guys you are awesome!!! @bumperbox Thanks for the inline explanation. That really helped me understand. – user2300933 Aug 28 '13 at 21:28
0

@Barmar comment is correct, you can't mix mysql_ and mysqli_ functions, which is why you are getting the error stated

I have also made some other changes to your code to simplify it. See inline comments for explanations

$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;
}
bumperbox
  • 10,166
  • 6
  • 43
  • 66
  • I am having an issue with this code now that I am trying to implement this into a wordpress plugin. I am using $result = $wpdb->get_results($sql); to return my results, but I think it is returning it as an array because I get the following error. "Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, array given in..." Is there a way to convert the array returned by $wpdb->get_results into a record set that this function can read? – user2300933 Sep 04 '13 at 14:25
  • can you do a var_dump($result) to make sure it is actually returning an array? – bumperbox Sep 04 '13 at 20:22