php - How to Prepare a Prepared Statement With Lots of If Statements? -


i have 25 optional field sql query. doing is, posting data using ajax (for pagination) php file prepare , execute query.

sample (index.php)

// optional value if (isset($_post['il']) && !empty($_post['il'])) $il = $_post['il']; // js change_page('0'); });  function change_page(page_id) {  var datastring = page_id; // define optional value  <?php if (isset($_post['il'])&& !empty($_post['il'])):?>  var il = '<?php echo $il;?>';  <?php endif; ?>   // send optional value  $.ajax({     type: "post",     url: "includes/post.php",     data: {     <?php if (isset($_post['il'])&& !empty($_post['il'])):?>'il': il,<?php endif; ?>  }, ... 

post.php (where operations handled)

... // main query (default) $bas_query = "select id, a, b, c, d, e test.db a=$1 , b=$2";  // if user has input 'il', add query.     if (isset($_post['il']) && !empty($_post['il'])) {         $il = $_post['il'];         $bas_query. = ' , city='.$il; }  // how it's executed after statements (without ifs included) $exec = pg_prepare($postgre,'sql2', 'select id, a, b, c, d, e test.db a=$1 , b=$2 offset $3 limit $4'); $exec = pg_execute($postgre,'sql2', array($1,$2,$3,$4)); 

my question is, how prepare prepared statement lot of if statements? can concatenate , prepare pg_prepare's query, how assign values before executing query in right order?

thanks in advance help.

try method

// change query below $bas_query = "select id, a, b, c, d, e test.db where";  // initiate array , counter prepared statement $data = array(); $counter = 1;  // assume assigned , b value same below ... // add data array each case if (isset($_post['il']) && !empty($_post['il']))     {     $il = $_post['il'];     $bas_query.= ' , city=$' . $counter;     $counter++;     $data[] = $il;     }  ... /* other if statements */ // prepare , execute statement; (after if statements) /* a) concatenate offset , limit end of final query    b) insert offset , limit value array       */ $bas_query.= ' offset $' . $counter . ' limit $' . ($counter + 1); $data[] = $your_offset_val; $data[] = $your_limit_val;  $exec = pg_prepare($postgre, 'sql2', $bas_query); $exec = pg_execute($postgre, 'sql2', $data); 

you have following output

select id, a, b, c, d, e test.db a=$1 , b=$2 , city=$3 offset $4 limit $5  array ( [0] => a_value [1] => b_value [2] => city_value [3] => offset_value [4] => limit_value ) 

Comments

Popular posts from this blog

Ansible - ERROR! the field 'hosts' is required but was not set -

customize file_field button ruby on rails -

SoapUI on windows 10 - high DPI/4K scaling issue -