brown2005 Posted February 15, 2007 Share Posted February 15, 2007 hi if i have $action = $_GET['action']; and $_GET['action'] = say cabbage can i have $sql = "SELECT * FROM $action WHERE $action_user = 'username';"; instead of $sql = "SELECT * FROM cabbage WHERE cabbage_user = 'username';"; Quote Link to comment https://forums.phpfreaks.com/topic/38615-using-action-in-a-string/ Share on other sites More sharing options...
ted_chou12 Posted February 15, 2007 Share Posted February 15, 2007 sure, why not? but you must have the quotations around any text you do, like "say cabbage". Ted Quote Link to comment https://forums.phpfreaks.com/topic/38615-using-action-in-a-string/#findComment-185309 Share on other sites More sharing options...
brown2005 Posted February 15, 2007 Author Share Posted February 15, 2007 $action = $_GET['action']; $url_sql = mysql_query("SELECT * FROM z_sponsors_adverts_hits_'.$action.' WHERE sponsors_adverts_hits_'.$action.'_type='0' AND sponsors_adverts_hits_'.$action.'_user='$config_ip' AND sponsors_adverts_hits_'.$action.'_advert='$website_id'", $connection); $url_rows = mysql_num_rows($url_sql); echo"rows = $url_rows"; like that? Quote Link to comment https://forums.phpfreaks.com/topic/38615-using-action-in-a-string/#findComment-185311 Share on other sites More sharing options...
JasonLewis Posted February 15, 2007 Share Posted February 15, 2007 this is incorrect syntax: $sql = "SELECT * FROM $action WHERE $action_user = 'username';"; Use it like this instead: $sql = "SELECT * FROM {$action} WHERE {$action}_user='username'"; you had an extra ; in your query and use { and } around your variables. Quote Link to comment https://forums.phpfreaks.com/topic/38615-using-action-in-a-string/#findComment-185313 Share on other sites More sharing options...
ted_chou12 Posted February 15, 2007 Share Posted February 15, 2007 a tip, when you have simple strings: $action , and having double quotations, you dont have to stop the quotation using '..', instead, simply place them like: "SELECT * FROM z_sponsors_adverts_hits_$action WHERE sponsors_adverts_hits..." as well as leave out the ; symbol, you dont need those in the query, it think thats about it... EDITED, do you have to do this: "SELECT * FROM {$action} WHERE {$action}_..."? i thought $action would work with " Ted Quote Link to comment https://forums.phpfreaks.com/topic/38615-using-action-in-a-string/#findComment-185315 Share on other sites More sharing options...
JasonLewis Posted February 15, 2007 Share Posted February 15, 2007 i dont think you have to, but sometimes if you dont it will give out a syntax error. Quote Link to comment https://forums.phpfreaks.com/topic/38615-using-action-in-a-string/#findComment-185316 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.