I've got a variable set at the top of my PHP:  $abc = "paid";   Now I need to pass it on to the below query as a column name. I tried the below but it did not work.  $sql = "SELECT customerID FROM payments WHERE :myVar = :credit"; $stmt = $connect-&gt;prepare($sql); $stmt-&gt;execute(array(':credit'=&gt;"0", ':myVar'=&gt;$abc));   How do I pass $abc on to the select query as a column name?
213
Why can't I pass the table name to a prepared PDO statement?  $stmt = $dbh-&gt;prepare('SELECT * FROM :table WHERE 1'); if ($stmt-&gt;execute(array(':table' =&gt; 'users'))) {     var_dump($stmt-&gt;fetchAll()); }   Is there another safe way to insert a table name into a SQL query? With safe, I mean that I don't want to do  $sql = "SELECT * FROM $table WHERE 1"
10527
In PHP you can just use $_POST for POST and $_GET for GET (Query string) variables. What's the equivalent in Python?
78620
Using AMPScript, I want to pass from a URL Attribute lookup variables and use these variables in my LookupOrderRows function.   The challenge is that not all lookup variables have to be passed by the URL e.g I might lookup just 'office' or I could also lookup both 'office' and 'score.   I've tried several approaches of Concat the string etc - but can't see to make the lookup work.  Has anyone had any success performing similar?   IF not empty( Requestparameter("office") ) then  Set @field1_name = "OFFICE" Set @field1_value = Requestparameter("office")  EndIf  IF not empty( Requestparameter("specialism") ) then  Set @field2_name = "SPECIALISM" Set @field2_value = Requestparameter("specialism")  EndIf  IF not empty( Requestparameter("score") ) then  Set @field3_name = "SCORE" Set @field3_value = Requestparameter("score")  EndIf   set @rows = LookupOrderedRows(@LeadsDE,0,"[Engagement Activity Date] desc",@Field1_Name,@Field1_Value,@Field2_Name,@Field2_Value,@Field3_Name,@Field3_Value)   My current solution is a lot If then statements and combinations i.e. :  /* OFFICE ONLY*/ IF NOT EMPTY(@input_office) AND EMPTY(@input_organisation) AND EMPTY(@input_specialism)  AND EMPTY(@input_score) then     set @rows = LookupOrderedRows(@LeadsDE,0,"[Engagement Activity Date] desc","OFFICE",@input_office) ENDIF  /* OFFICE + ORG*/ IF NOT EMPTY(@input_office) AND NOT EMPTY(@input_organisation) AND EMPTY(@input_specialism)  AND EMPTY(@input_score) then     set @rows = LookupOrderedRows(@LeadsDE,0,"[Engagement Activity Date] desc","OFFICE",@input_office,"ORGANISATION_NAME",@input_organisation) ENDIF /* OFFICE + ORG + SPECIALISM */ IF NOT EMPTY(@input_office) AND NOT EMPTY(@input_organisation) AND NOT EMPTY(@input_specialism)  AND EMPTY(@input_score) then     set @rows = LookupOrderedRows(@LeadsDE,0,"[Engagement Activity Date] desc","OFFICE",@input_office,"ORGANISATION_NAME",@input_organisation,"SPECIALISM",@input_specialism) ENDIF /* OFFICE + ORG + SCORE */ IF NOT EMPTY(@input_office) AND NOT EMPTY(@input_organisation) AND EMPTY(@input_specialism)  AND NOT EMPTY(@input_score) then     set @rows = LookupOrderedRows(@LeadsDE,0,"[Engagement Activity Date] desc","OFFICE",@input_office,"ORGANISATION_NAME",@input_organisation,"Engagement Score",@input_score) ENDIF /* OFFICE + ORG + SPECIALISM + SCORE  */ IF NOT EMPTY(@input_office) AND NOT EMPTY(@input_organisation) AND NOT EMPTY(@input_specialism)  AND EMPTY(@input_score) then     set @rows = LookupOrderedRows(@LeadsDE,0,"[Engagement Activity Date] desc","OFFICE",@input_office,"ORGANISATION_NAME",@input_organisation,"SPECIALISM",@input_specialism,"Engagement Score",@input_score) ENDIF  IF NOT EMPTY(@input_office) AND EMPTY(@input_organisation) AND NOT EMPTY(@input_specialism)  AND EMPTY(@input_score) then     set @rows = LookupOrderedRows(@LeadsDE,0,"[Engagement Activity Date] desc","OFFICE",@input_office,"SPECIALISM",@input_specialism) ENDIF  IF NOT EMPTY(@input_office) AND EMPTY(@input_organisation) AND NOT EMPTY(@input_specialism)  AND NOT EMPTY(@input_score) then     set @rows = LookupOrderedRows(@LeadsDE,0,"[Engagement Activity Date] desc","OFFICE",@input_office,"SPECIALISM",@input_specialism,"Engagement Score",@input_score) ENDIF  IF NOT EMPTY(@input_office) AND EMPTY(@input_organisation) AND EMPTY(@input_specialism)  AND NOT EMPTY(@input_score) then     set @rows = LookupOrderedRows(@LeadsDE,0,"[Engagement Activity Date] desc","OFFICE",@input_office,"Engagement Score",@input_score) ENDIF
82767
I'm attempting to do this in a procedure:  DECLARE @a bit = 1; BEGIN     SELECT * INTO #aTemp FROM OPENROWSET( ... );      IF @a = 0     BEGIN         SELECT ... INTO #bTemp FROM #aTemp;     END     ELSE     BEGIN         SELECT ... INTO #bTemp FROM #aTemp;     END END   I get the error:  Msg 2714, Level 16, State 1, Line 10 There is already an object named '#bTemp' in the database.   Why is this happening and is there a work around?  Update  I've attempted to add a DROP statement as suggested , but it still does not work:  DECLARE @a bit = 1; BEGIN     SELECT * INTO #aTemp FROM OPENROWSET( ... );      IF @a = 0     BEGIN         IF OBJECT_ID('[tempdb]..#bTemp') IS NOT NULL         BEGIN             DROP TABLE #bTemp;         END          SELECT ... INTO #bTemp FROM #aTemp;     END     ELSE     BEGIN         IF OBJECT_ID('[tempdb]..#bTemp') IS NOT NULL         BEGIN             DROP TABLE #bTemp;         END          SELECT ... INTO #bTemp FROM #aTemp;     END END
112662
echo $_POST["name"]; //returns the value a user typed into the "name" field   I would like to be able to also return the text of the key. In this example, I want to return the text "name". Can I do this?
31080
Are PHP variables passed by value or by reference?
85564
From the :  isset() will return FALSE if testing a variable that has been set to NULL.   Basically, isset() doesn't check for whether the variable is set at all, but whether it's set to anything but NULL.  Given that, what's the best way to actually check for the existence of a variable?  I tried something like:  if(isset($v) || @is_null($v))   (the @ is necessary to avoid the warning when $v is not set) but is_null() has a similar problem to isset(): it returns TRUE on unset variables!  It also appears that:  @($v === NULL)   works exactly like @is_null($v), so that's out, too.  How are we supposed to reliably check for the existence of a variable in PHP?    Edit: there is clearly a difference in PHP between variables that are not set, and variables that are set to NULL:  &lt;?php $a = array('b' =&gt; NULL); var_dump($a);   PHP shows that $a['b'] exists, and has a NULL value.  If you add:  var_dump(isset($a['b'])); var_dump(isset($a['c']));   you can see the ambiguity I'm talking about with the isset() function.  Here's the output of all three of these var_dump()s:  array(1) {   ["b"]=&gt;   NULL } bool(false) bool(false)     Further edit: two things.  One, a use case.  An array being turned into the data of an SQL UPDATE statement, where the array's keys are the table's columns, and the array's values are the values to be applied to each column.  Any of the table's columns can hold a NULL value, signified by passing a NULL value in the array.  You need a way to differentiate between an array key not existing, and an array's value being set to NULL; that's the difference between not updating the column's value and updating the column's value to NULL.  Second, , array_key_exists() works correctly, for my above use case and for any global variables:  &lt;?php $a = NULL; var_dump(array_key_exists('a', $GLOBALS)); var_dump(array_key_exists('b', $GLOBALS));   outputs:  bool(true) bool(false)   Since that properly handles just about everywhere I can see there being any ambiguity between variables that don't exist and variables that are set to NULL, I'm calling array_key_exists() the official easiest way in PHP to truly check for the existence of a variable.  (Only other case I can think of is for class properties, for which there's property_exists(), which, according to , works similarly to array_key_exists() in that it properly distinguishes between not being set and being set to NULL.)
83334
I've seen a number of people claim that you should specifically name each column you want in your select query.  Assuming I'm going to use all of the columns anyway, why would I not use SELECT *?  Even considering the question **, I don't think this is an exact duplicate as I'm approaching the issue from a slightly different perspective.    One of our principles is to not optimize before it's time.  With that in mind, it seems like using SELECT * should be the preferred method until it is proven to be a resource issue or the schema is pretty much set in stone.  Which, as we know, won't occur until development is completely done.  That said, is there an overriding issue to not use SELECT *?
14520
For example, mysql quote table name using   SELECT * FROM `table_name`;   notice the `   Does other database ever use different char to quote their table name
21318
I tried searching around, but I couldn't find anything that would help me out.  I'm trying to do this in SQL:  declare @locationType varchar(50); declare @locationID int;  SELECT column1, column2 FROM viewWhatever WHERE CASE @locationType     WHEN 'location' THEN account_location = @locationID     WHEN 'area' THEN xxx_location_area = @locationID     WHEN 'division' THEN xxx_location_division = @locationID   I know that I shouldn't have to put '= @locationID' at the end of each one, but I can't get the syntax even close to being correct. SQL keeps complaining about my '=' on the first WHEN line...  How can I do this?
40938
After I posted this question, I found a similar post that seemed to answer it: . I'll leave this in place for now, as I am not sure how to mark my own question as a duplicate  In attempting to debug a SOQL query error, I encountered a strange behavior with Database.query and bind variables. Specifically, when I run this code:  String userQuery = 'SELECT Id FROM User'; if (true) {     List&lt;String&gt; firstNames = new List&lt;String&gt;{'James','Joe'};     userQuery += ' WHERE FirstName IN :firstNames'; } Database.query(userQuery);   the Database.query statement executes with no errors, despite the fact that the variable firstNames does not exist in its context. However, when I add an else clause:  String userQuery = 'SELECT Id FROM User'; if (true) {     List&lt;String&gt; firstNames = new List&lt;String&gt;{'James','Joe'};     userQuery += ' WHERE FirstName IN :firstNames'; } else {     List&lt;String&gt; firstNames; } Database.query(userQuery);   I receive a null object reference exception. It seems like Salesforce is checking to see if the variable is only declared once beforehand and, if so, uses its value in the context it was declared. This seems strange to me, so I was wondering if anybody here has any insight into what is going on under the hood.
8309
I need to query the database to get the column names, not to be confused with data in the table.  For example, if I have a table named EVENT_LOG that contains eventID, eventType, eventDesc, and eventTime, then I would want to retrieve those field names from the query and nothing else.  I found how to do this in:        But I need to know: how can this be done in Oracle?
82651
I have an output of a query as follows:  bid  | code | amount        | ----------------------------- 2915 | BF   |   -10700.00   | 2915 | YQ   |   -300.00     | 2915 | YR   |   0.00        | 2915 | YM   |   0.00        | 2915 | WO   |   -153.00     | 2915 | IN   |   -329.00     | 2915 | K3   |   0.00        | 2915 | CUTE |   -50.00      | -----------------------------   I need to convert the values in code column to individual columns with column name as corresponding code and amount shall be the value.  bid  | BF       | YQ     | ... | CUTE  | ---------------------------------------- 2915 | -10700.00| -300.00| ... | -50.00|   The issue is that the result of my initial query has dynamic codes and hence need to generate columns accordingly.  Already tried the following logic but couldn't achieve the goal:   , ,   All the above links use some expression or the other to generate the columns.   My original resulting rows are result of simple select on multiple joined tables, and has no calculation or expression scope.  Kindly suggest some pointer or hint on how to achieve this.  Many thanks in advance.
52590
When I have a Set of Id's and I want to do a dynamic SOQL query that I want to use it in for an IN comparison, I have to convert the Set to string in the format of:  ('id1','id2',id3',...)   The way I do it works, but is not particularly elegant, so I'm wondering if anyone has come up with a better way. Am I missing something? Is there something out there that can just turn a set or list into the properly formatted string?   Here's my code (yes, my example isn't dynamic SOQL, but I've used this several times in dynamic SOQL):   Map&lt;Id,Account&gt; accts = new Map&lt;Id,Account&gt;([select Id from Account]);  String idString = '(\''; for (Id thisId : accts.keyset()) {     idString += thisId + '\',\''; } idString = idString.substring(0,idString.length()-2); //&lt;--this is the bit I hate doing idString += ')';  String q = 'select id from Contact where AccountId in ' + idString;  List&lt;Contact&gt; cts = Database.query(q);
25709
Let's say I have code like this:  $dbh = new PDO("blahblah");  $stmt = $dbh-&gt;prepare('SELECT * FROM users where username = :username'); $stmt-&gt;execute( array(':username' =&gt; $_REQUEST['username']) );   The PDO documentation says:     The parameters to prepared statements don't need to be quoted; the driver handles it for you.   Is that truly all I need to do to avoid SQL injections?  Is it really that easy?  You can assume MySQL if it makes a difference.  Also, I'm really only curious about the use of prepared statements against SQL injection.  In this context, I don't care about XSS or other possible vulnerabilities.
42072
I have a table called MyTable with varchar columns named SourceID and ReferenceID, where SourceID contains a table name and ReferenceID contains a key value for the source table. Is it possible in a query to refer to the source table's row assuming I know the name of the key column that matches the value in RefrenceID? Sample: SELECT     * FROM     MyTable as MT INNER JOIN MT.SourceID as Src ON MT.ReferenceID = Src.InstanceID  This doesn't work, obviously.  But is there a way to do this as opposed to dynamically building the SQL statement using the values of SourceID and ReferenceID and then executing the query?  More specifically, is there a way to make MyTable.SourceID in the example above be usable within the JOIN? Further considerations I found , and it has a trick like follows: select   E.EmployeeName, coalesce(s.store,o.office) as Location from   Employees E left outer join    Stores S on ... left outer join   Offices O on ... where   O.Office is not null OR S.Store is not null  The WHERE clause simply helps the outer joins behave like INNER JOINs.  This would work well enough if there were a limited number of table options and they all had UNIQUE constraints.  This gets me part of the way there but but I really want is to have the table names in the JOINs to be based on a column in Employees. I haven't given up yet. Any and all help is greatly appreciated.
89206
I am getting error:     The argument 2 of the "JSON_VALUE or JSON_QUERY" must be a string literal   ...while trying to pass variable as parameter to JSON_QUERY function. Please help me to resolve it.  DECLARE @cnt INT = 1; DECLARE @cnt_total INT = 1; DECLARE @json NVARCHAR(MAX); DECLARE @json1 NVARCHAR(MAX); declare @str  VARCHAR(200);  WHILE @cnt &lt;= 10 BEGIN     set @str = '$.Seasons[0].Products['+convert(varchar,@cnt)+'].ProductChannels';     set @str = char(39) +@str+ char(39);     PRINT @STR     select @json = json_query(jfile,@STR) from Import.tstjson; --MORE CODE         SET @cnt = @cnt + 1; END   Sample data:  {   "Seasons": [     {       "Season": "12321231",       "Products": [         {           "ProductId": "211",           "ProductChannels": [             {               "ChannelId": 1,               "WeekQuantities": []             }           ],           "ccc": 3,           "Tttt": 4         }       ]     }   ] }