2013-11-25 83 views
-1

我在codeigniter中遇到UNION查詢問題。它給出的輸出,但名稱是相同的數組中的兩列。Codeigniter UNION查詢問題

$sql = "SELECT firstname FROM user_profile WHERE user_id ='".$id."' 

UNION SELECT email FROM user WHERE id='".$id."'"; 

$data = $this->db->query($sql); 

print_r($data->result()); 

,這是輸出:

Array 
(

    [0] => stdClass Object 
     (
      [firstname] => Dimpy 
     ) 

    [1] => stdClass Object 
     (
      [firstname] => [email protected] 
     ) 

) 
+0

請通過[PDO](http://php.net/pdo)或[PDO](http://php.net/pdo)學習如何使用[** prepared statements **](https://www.youtube.com/watch?v=nLinqtCfhKY)庫MySQLi](http://php.net/mysqli)。 –

+0

Teresko,他使用codeigniter。開始將自己的MySQL驅動程序加入其中並不實際。 不過Dimple,你應該確定你使用的數據沒有被瀏覽器解析。 – andershagbard

+0

當你需要分開的列使用連接而不是聯合 –

回答

1
$sql = "SELECT t1.firstname,t2.email FROM user_profile as t1 join user as t2 on t1.id = t2.id where WHERE t2.id='".$id."'";