mysql - PHP - Use Field Names as Variable -
i getting error "warning: mysql_field_name() expects parameter 1 resource, object given in... on line 28"
i new php, trying accomplish read html file , replace custom tag value of record. tag structure |+field-name-record#+| example if sql returns 2 records "name" field following 2 tags |+name1+| , |+name2+| in html file , replace 2 returned values. adam , jim.
below code have far.
$c = '|+name1+|<br>|+name2+|'; echo(replacehtmlvariables(mysqli_query($con,"select * nc_names"),$c)); function replacehtmlvariables($results, $content){ while($row = mysqli_fetch_array($results)){ //define variable count record on $rnum = 1; //loop through fields in record $field = count( $row ); ( $i = 0; $i < $field; $i++ ) { //use field name , record number create variables replace current record value $content = str_replace("|+".mysql_field_name( $results, $i ).$rnum."+|",$row[$i],$content); } //move next record $rnum++; } return $content; }
line 28 references line
$content = str_replace("|+".mysql_field_name( $results, $i ).$rnum."+|",$row[$i],$content);
you're mixing mysql mysqli, , don't need if pull mysqli_fetch_assoc() instead:
function replacehtmlvariables($results, $content) { //define variable count record on $rnum = 1; /*** using mysqli::fetch_assoc() ***/ while( $row = mysqli_fetch_assoc( $results ) ) { //loop through fields in record /*** variable $value passed reference performance ***/ foreach( $row $fieldname => &$value ) { $content = str_replace("|+".$fieldname.$rnum."+|",$value,$content); } ++$rnum; /*** faster $rnum++ **/ } return $content; }
mysqli_fetch_assoc()
pulls data associative array, field name index key.
Comments
Post a Comment