mysql - php variable as the column name in mysql_fetch_assoc variable -


the following code shows error : undefined index: $col_name in ...

$table="my_table"; $col_name="my_col";  $sql="select * $table";  $result=mysql_query($sql);  while($row=mysql_fetch_assoc($result)){  $db_value = trim(htmlspecialchars_decode($row['$col_name']));  } 

but if replace last line inside while loop following 1 :

$db_value = trim(htmlspecialchars_decode($row['my_col'])); 

it's ok.

how can use php variable column name instead of using column name directly ?

use double quotes,

$db_value = trim(htmlspecialchars_decode($row["$col_name"])); 

or here can avoid quotes,

$db_value = trim(htmlspecialchars_decode($row[$col_name])); 

read strings more quotes.


Comments

Popular posts from this blog

.htaccess - First slash is removed after domain when entering a webpage in the browser -

c# - Farseer ContactListener is not working -

Automatically create pages in phpfox -