2013-08-25 75 views
0

我使用codeigniter來創建網站。而我需要的結果從MySQL表作爲 這如何從codeigniter模型獲取結果作爲對象

object(mysqli_result)#2 (5) { ["current_field"]=> int(0) ["field_count"]=> int(4) ["lengths"]=> NULL ["num_rows"]=> int(12) ["type"]=> int(0) } 

但始終我得到從這個結果的東西傳出。

當我使用result_object()在笨我得到這樣的事情

array(12) { [0]=> object(stdClass)#24 (4) { ["id"]=> string(1) "1" ["label"]=> string(15) "Web Development" ["link_url"]=> string(0) "" ["parent_id"]=> string(1) "0" } [1]=> object(stdClass)#25 (4) { ["id"]=> string(2) "10" ["label"]=> string(19) "Sales and Marketing" ["link_url"]=> string(0) "" ["parent_id"]=> string(1) "0" } [2]=> object(stdClass)#26 (4) { ["id"]=> string(1) "7" ["label"]=> string(18) "Design and Artwork" ["link_url"]=> string(0) "" ["parent_id"]=> string(1) "0" } [3]=> object(stdClass)#27 (4) { ["id"]=> string(1) "2" ["label"]=> string(16) "Content Creation" ["link_url"]=> string(0) "" ["parent_id"]=> string(1) "0" } [4]=> object(stdClass)#28 (4) { ["id"]=> string(1) "4" ["label"]=> string(19) "OSCommerce projects" ["link_url"]=> string(29) "/php_web_development_jobs.php" ["parent_id"]=> string(1) "1" } [5]=> object(stdClass)#29 (4) { ["id"]=> string(1) "3" ["label"]=> string(8) "PHP Jobs" ["link_url"]=> string(29) "/php_web_development_jobs.php" ["parent_id"]=> string(1) "1" } [6]=> object(stdClass)#30 (4) { ["id"]=> string(1) "5" ["label"]=> string(22) "Technical Writing Jobs" ["link_url"]=> string(29) "/php_web_development_jobs.php" ["parent_id"]=> string(1) "2" } [7]=> object(stdClass)#31 (4) { ["id"]=> string(1) "6" ["label"]=> string(13) "Forum Posting" ["link_url"]=> string(29) "/php_web_development_jobs.php" ["parent_id"]=> string(1) "2" } [8]=> object(stdClass)#32 (4) { ["id"]=> string(1) "8" ["label"]=> string(20) "Blog Design Projects" ["link_url"]=> string(29) "/php_web_development_jobs.php" ["parent_id"]=> string(1) "7" } [9]=> object(stdClass)#33 (4) { ["id"]=> string(1) "9" ["label"]=> string(24) "Freelance Website Design" ["link_url"]=> string(29) "/php_web_development_jobs.php" ["parent_id"]=> string(1) "7" } [10]=> object(stdClass)#34 (4) { ["id"]=> string(2) "11" ["label"]=> string(29) "Internet Marketing Consulting" ["link_url"]=> string(29) "/php_web_development_jobs.php" ["parent_id"]=> string(2) "10" } [11]=> object(stdClass)#35 (4) { ["id"]=> string(2) "12" ["label"]=> string(25) "Leads Generation Services" ["link_url"]=> string(29) "/php_web_development_jobs.php" ["parent_id"]=> string(2) "10" } } 


$this->db->order_by('parent_id','id','ASC'); 
$query = $this->db->get('dyn_menu'); 
if ($query->num_rows() > 0) { 
    $data = $query->result_object(); 
    return $data; 
} 

我怎樣才能得到結果,因爲我想要的嗎?

+0

請注意這兩個結果是完全不同的。由於它們具有不同的列,因此它們似乎不是從同一張表中獲取的。 –

回答

3

嘗試使用剛剛result(),如:

... 
$data = $query->result(); 
return $data; 
... 
+0

或'return $ query-> result();':) – Shomz

+0

仍然是一樣的:( – Yasitha

2

嘗試你想這個,如果單個記錄,使$ is_single =真

if($is_single) 
    return $query ->row_array(); 
else 
    return $query ->result_array(); 
+0

不知道如何得到upvotes當它說它需要返回對象... – Shomz

+0

@Shomz他已經得到了他的答案我給了更多更聰明的方法。他可以通過傳遞模式添加另一個返回值,返回$ query-> result(); –

相關問題