2017-04-17 104 views
2

代碼:如何顯示數據,如果一個字段具有整數值以及在php/mysql中的文本?

<?php 
if($affiliated == '') 
{ 
    echo $affiliated; 
} 
else 
{ 
    $sql = "select * from all_university where university_id = '$affiliated'"; 
    $result = mysqli_query($link,$sql); 
    while ($row = mysqli_fetch_array($result)) 
    { 
     $short_name = $row['short_name']; 
    } 
} 

>

在我有數據庫表字段名下屬和具有不同行中兩個不同的值有「整」值一列,並有另一種「文本」的價值?。我想要顯示不同的值,即如果$ affiliated具有整數值,它會打印整數其他打印文本。那麼,我該如何解決這個問題呢?

謝謝

+0

你嘗試過什麼到目前爲止,檢查我的意見? – hassan

+0

你可以顯示你的嘗試@kevin –

回答

0

首先,你需要檢查$affiliatednumeric與否,並在每一行

//check for integer 
    if (is_numeric($affiliated)) { 
     //condition for integer 
     // any number multiply by 1 equal to that number 
     $where = " WHERE concat('',university_id * 1) = university_id"; 
    } else { 
     //condition for text 
     $where = " WHERE concat('',university_id * 1) != university_id";// here use not equal to condition 
    } 
    $sql = "select * from all_university" . $where;// pass $where accouding to avove condition 
    $result = mysqli_query($link, $sql); 
    while ($row = mysqli_fetch_array($result)) { 
     $short_name = $row['short_name']; 
    } 
+0

謝謝薩蒂。其工作 – kevin

+0

你好!歡迎! – Saty

相關問題