0
votes

i am trying to use the following select query in my php code where all var1,var2,var3,var4,var5 are array variables but its not working

 $sql="SELECT SNo,Date,FROM_TIME,TO_TIME,$var1,$var2,$var3,$var4,$var5 
 FROM $table_name 
 WHERE Date='$start_date'";
1
Define "not working". Also, why? There's a reason prepared queries (which you should be using, hint hint!) don't let you do this. - Niet the Dark Absol
tell us the (mysql?) error - albertdiones
"Array variables"? Have a look at this: stackoverflow.com/questions/907806/… - TribalChief
is there one array, so you should use $var[0], $var[1], $var[2], ... or are there more arrays, e.g. $var1[0], $var2[0], $var3[0], ... ? - SaschaP

1 Answers

0
votes

You can build your mysql select query using array value by this way

<?php 
$columnArray=array('column1'=>'value1',
        'column2'=>'value2', 
        'column3'=>'value3',
        'column4'=>'value4');
        //print_r($columns);
         $columns = implode(',',array_keys($columnArray));
        if(count($columnArray)>0)
         $comma=',';
        else 
          $comma='';

       $sql="SELECT SNo,Date,FROM_TIME,TO_TIME" .$comma." $columns
          FROM tableName
          WHERE Date='2014-06-22'";
        print $sql; //final query
        ?>