CodeIgniter 数据库连接查询

来源:互联网 发布:手机淘宝怎样退货流程 编辑:程序博客网 时间:2024/05/21 22:36

刚接触CI ,要操作数据库一直没有找到对的方式。

环境:wampserverCI

问题:从phpmyadmin中的表中查询数据,报错:

1、Message:Object of class CI_DB_mysqli_result could not be converted to string

2、Message:Array to string conversion

3、Message: syntax error, unexpected ''name''(T_CONSTANT_ENCAPSED_STRING), expectT_CONSTANT_ENCAPSED_STRING

原因:

数据库中存储的数据类型多,想要取带类型的数据,环境不一定支持,所以PHP与mysql之间的通讯全用string。

而$query=$this->db->query($sql); 返回数据为object型。要从$query中取出结果,并按字段进行查询。具体代码如下所示。

<?phpclass Dbtest extends CI_Controller{public function index(){echo 'what a good day!';}public function dbcon(){$this->load->database();$sql = "SELECT * FROM clientcount where id =3";$query = $this->db->query($sql); if ( ! $this->db->simple_query($sql)){    $error = $this->db->error(); } if($query->num_rows()>0){$result=$query->result(); foreach($result as $row){echo $row->id;echo $row->name;}}else{echo false;} }}?>


0 0
原创粉丝点击