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);
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.
@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;
}