php_begins Posted September 1, 2011 Share Posted September 1, 2011 Hi, In some cases I need to execute a default query , so I am storing the SELECT statement in a variable like this and executing it: $default_query="SELECT * from user where userid='$userid'"; $user_query=mysql_query($default_query); The above code returns an empty results. But if I execute it without the variable it works fine. $user_query=mysql_query("SELECT * from user where userid='$userid'"); Am I syntatically wrong somewhere? Quote Link to comment https://forums.phpfreaks.com/topic/246187-executing-a-query-stored-in-a-variable/ Share on other sites More sharing options...
jcbones Posted September 1, 2011 Share Posted September 1, 2011 You should be coding with error_reporting and display errors set to full, and all possible de-bugging in place. For queries, you should have. <?php error_reporting(-1); ini_set('display_errors',1); $default_query="SELECT * from user where userid='$userid'"; $user_query=mysql_query($default_query) or trigger_error($default_query . ' has an error<br />' . mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/246187-executing-a-query-stored-in-a-variable/#findComment-1264354 Share on other sites More sharing options...
PFMaBiSmAd Posted September 1, 2011 Share Posted September 1, 2011 You would need to post your complete and actual code that does not work. I can tell, based on experience, from the symptom, that you either have some lines of code between where you are setting the variable and where you are using it that is clearing it or your mysql_query statement is in a different program scope from where you set the variable. You also don't have php's error reporting set to E_ALL and display_errors set to ON to get php to report and display all the errors it detects and you don't have any error checking logic on the mysql_query statement to get it to tell you why it is failing. Edit: Which is pretty much exactly what jcbones posted above. Quote Link to comment https://forums.phpfreaks.com/topic/246187-executing-a-query-stored-in-a-variable/#findComment-1264356 Share on other sites More sharing options...
php_begins Posted September 1, 2011 Author Share Posted September 1, 2011 ya i tried that..it still provides me with an empty result when i try to execute the default query.. Quote Link to comment https://forums.phpfreaks.com/topic/246187-executing-a-query-stored-in-a-variable/#findComment-1264361 Share on other sites More sharing options...
php_begins Posted September 1, 2011 Author Share Posted September 1, 2011 ok never mind..it was not getting the $userid variable since I was using that variable before I retrived the $userid from the database! Quote Link to comment https://forums.phpfreaks.com/topic/246187-executing-a-query-stored-in-a-variable/#findComment-1264365 Share on other sites More sharing options...
PFMaBiSmAd Posted September 1, 2011 Share Posted September 1, 2011 If your query is not producing an error, then the problem is something else that your code is doing. If you want help with what your code is doing or not doing, you need to post enough of it that duplicates the problem. Quote Link to comment https://forums.phpfreaks.com/topic/246187-executing-a-query-stored-in-a-variable/#findComment-1264366 Share on other sites More sharing options...
PFMaBiSmAd Posted September 1, 2011 Share Posted September 1, 2011 ok never mind..it was not getting the $userid variable since I was using that variable before I retrived the $userid from the database! Setting the error_reporting and display_errors settings as suggested would have directly pointed out a variable that was not set at the time you referenced it. Quote Link to comment https://forums.phpfreaks.com/topic/246187-executing-a-query-stored-in-a-variable/#findComment-1264377 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.