如何传递null&通过函数填充值到sql字符串

时间:2014-02-04 00:53:40

标签: php

如何通过函数将空值或填充值传递给SQL字符串...

SELECT id FROM dod WHERE department = 'OPS' AND dod_1 = '1'
    AND dod_2 = 'a' AND dod_3 = '3' AND dod_4 = '' AND dod_5 = ''
function insertTable2Link($db, $var_1, $var_2 = NULL, $var_3 = NULL, $var_4 = NULL, $var_5 = NULL) {

    $query_select = pg_query($db, "SELECT id FROM abc WHERE dod_1 = '{$var_1}' AND dod_2 = '{$var_2}' AND dod_3 = '{$var_3}' AND dod_4 = '{$var_4}' AND dod_5 = '{$var_5}'");

}

在此示例中,有时我需要dod_5 IS NULL其他时间我需要dod_5 = '133'

2 个答案:

答案 0 :(得分:0)

SELECT id FROM abc WHERE dod_1 = '{$var_1}' AND dod_2 = '{$var_2}' AND dod_3 = '{$var_3}' AND dod_4 = '{$var_4}' AND (dod_5 = '{$var_5}' OR dod_5 IS NULL);

答案 1 :(得分:0)

如果你想在数据库中传递空值,那么你应该这样做

if(isset($_POST['submit'])){
  $name = $_POST["name"];
  if($_POST['age']==''){
     $data= 'NULL';
  }else{
     $data=$_POST["age"];
  }
  $ins="insert into demonull(`name`,`age`)values('".$name."','".$data."')";
  mysql_query($ins);
}