KJS KJS - 3 months ago 17
MySQL Question

Auto generate mysqli_fetch_array column results

I am trying to auto-generate the mysqli_fetch_array column results, like:

$column_name=$rows["column_name"];


The values are stored in array '$lang_news_cols_arr', used as:

foreach($lang_news_cols_arr as $lang_news_col_name) {
$news_rows .= '$'.$lang_news_col.'=$rows["'.$lang_news_col.'"];';
}


$news_rows would then have the following result:

$column_name_1=$rows["column_name_1"];
$column_name_2=$rows["column_name_2"];
$column_name_3=$rows["column_name_3"];
etc...


And finally be used as:

while ($rows = mysqli_fetch_array ($query) ) {
$news_rows;
}


I can't get the values to the $news_rows in the fetch array. Any help is very appreciated.

Answer

$news_rows is a string variable, php will not execute its contents as code without being explicitly instructed to do so. You could technically do this by calling the eval() function, but you really should not do this, since it introduces a huge security risk. eval() can execute any arbitrary php code, thus if a malicious user manages to inject their own code into $news_rows, then eval() will run that, too.

I would simply use an associative array to store the results, where the keys would be the column names.

Comments