【问题标题】:how to get result from codeigniter model as object如何从codeigniter模型中获取结果作为对象
【发布时间】:2013-08-25 05:48:14
【问题描述】:

我正在使用 codeigniter 创建一个网站。我需要从mysql表中获取结果 这个

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

但我总是从中得到一些东西。

当我在 codeigniter 中使用 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;
}

我怎样才能得到我想要的结果?

【问题讨论】:

  • 请注意这两个结果是完全不同的。由于它们具有不同的列,因此它们似乎不是从同一个表中获取的。

标签: php mysql codeigniter


【解决方案1】:

尝试只使用result(),例如:

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

【讨论】:

  • return $query->result(); :)
【解决方案2】:

如果你想要单条记录,试试这个,让 $is_single = true

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

【讨论】:

  • @Shomz 他已经得到了答案,我给出了更聪明的方法。他可以通过传递模式添加另一个返回值,return $query->result();
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-08-15
  • 2018-08-06
  • 2012-03-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多