sql中對於傳過來的空值判斷,該怎麼處理

來源:互聯網
上載者:User
sql中對於傳過來的空值判斷
我有四個這樣的輸入值
action到處理的php頁面
有沒有什麼方法能夠合理的讓php頁面的sql語句判斷當那個欄位為空白時,就不以那個為搜尋的條件了
除了窮舉,還有什麼方法?
$sql="select * from table where ..."


執行個體:
html


chuli.php
//串連資料庫部分省略
$sql="select * from student where name='$name' and age='$age' and sex='$sex' and height='$height';// 這個是所有的
$result = mysql_query($sql,$con);

//除了窮舉以外,有沒有什麼方法,判斷那幾個Input資料為空白的時候的$sql進行判斷的方法?
//如果是4個input的情況下的時候是 4+6+4+1+1=16種情況,應該對吧? c41+c42+c43+c44+1=4+6+4+1+1=16?

------解決方案--------------------
PHP code
foreach(array_diff($_POST, array('')) as $k=>$v)  $r[] = "$k='$v'";$sql = "select * from student where ". join(' and ', $r);
------解決方案--------------------
應該是,上面少給一個''

PHP code
$sql="select * from student where 1=1";if(!empty($_POST['name'])) $sql=" and name='$_POST[name]'";if(!empty($_POST['age'])) $sql=" and age='$_POST[age]'";if(!empty($_POST['sex'])) $sql=" and sex='$_POST[sex]'";if(!empty($_POST['height'])) $sql=" and height='$_POST[height]'";
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.