WebsoluteSW Posted September 5, 2013 Share Posted September 5, 2013 Hello, I'm working onthe below peice of code basically when i call a table from the database I dont want to have to write all the code each time hence the function, The only part I'm having trouble with is specifying the column heading without hard codeing it see below. mysql_fetch_assoc function return_table_item($item, $table) { $table_item="SELECT $item FROM $table"; $table_item_results =mysql_query($table_item); while ($item_return=mysql_fetch_assoc($table_item_results)){ echo '<a href=' . $item_return['$item'] . '.php target="menu_results"><li>' . sanitize_text_from_tables($item_return['$item']]) . '</li></a>'; } how can I request the $item column using the function variable $item. Any help appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/281881-need-help-with-functions-mysql_fetch_assoc/ Share on other sites More sharing options...
cyberRobot Posted September 5, 2013 Share Posted September 5, 2013 Variables don't work within single-quoted strings. In this case the quotes could be removed: $item_return[$item] Quote Link to comment https://forums.phpfreaks.com/topic/281881-need-help-with-functions-mysql_fetch_assoc/#findComment-1448270 Share on other sites More sharing options...
WebsoluteSW Posted September 5, 2013 Author Share Posted September 5, 2013 echo '<a href=' . $item_return[$item] . '.php target="menu_results"><li>' . sanitize_text_from_tables($item_return[$item]]) . '</li></a>'; PHP Parse error: syntax error, unexpected ']' in /home/quotegen/public_html/QuoteGenV4/library/class_lib.php on line 19 Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/281881-need-help-with-functions-mysql_fetch_assoc/#findComment-1448272 Share on other sites More sharing options...
WebsoluteSW Posted September 5, 2013 Author Share Posted September 5, 2013 Whoops my bad just saw the issue, thanks for your help! Quote Link to comment https://forums.phpfreaks.com/topic/281881-need-help-with-functions-mysql_fetch_assoc/#findComment-1448273 Share on other sites More sharing options...
cyberRobot Posted September 5, 2013 Share Posted September 5, 2013 No problem, glad to help. Side note: Perhaps you are already aware of this, but the mysql_ functions have been depreciated. More information about the alternatives can be found here: http://www.php.net/manual/en/mysqlinfo.api.choosing.php Quote Link to comment https://forums.phpfreaks.com/topic/281881-need-help-with-functions-mysql_fetch_assoc/#findComment-1448274 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.