codeIgniter3 学习笔记十(数据库操作)
Posted 岁月可贵
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了codeIgniter3 学习笔记十(数据库操作)相关的知识,希望对你有一定的参考价值。
数据库操作
class Db extends CI_Controller
public function __construct()
parent::__construct();
$this->load->database();
// 多结果查询(对象形式)
public function select_object()
$query = $this->db->query('SELECT * FROM user');
foreach ($query->result() as $row)
echo $row->id;
echo $row->user;
echo $row->password;
echo "<br/>";
// 多结果查询(数组形式)
public function select_array()
$query = $this->db->query('SELECT * FROM user');
foreach ($query->result_array() as $row)
echo $row['id'];
echo $row['user'];
echo $row['password'];
echo "<br/>";
// 单结果查询(对象形式)
public function select_limit_object()
$query = $this->db->query('SELECT * FROM user LIMIT 1');
$row = $query->row();
echo $row->id;
echo $row->user;
echo $row->password;
// 单结果查询(数组形式)
public function select_limit_array()
$query = $this->db->query('SELECT * FROM user LIMIT 1');
$row = $query->row_array();
echo $row['id'];
echo $row['user'];
echo $row['password'];
// 插入数据
public function insert()
$user = "好好";
$password = "浩浩";
$sql = "INSERT INTO user (user, password) VALUES (".$this->db->escape($user).", ".$this->db->escape($password).")";
$this->db->query($sql);
echo $this->db->affected_rows();
// 构造器模式查询
public function construct_select()
$query = $this->db->get("user");
foreach ($query->result() as $item)
echo $item->id;
echo $item->user;
echo $item->password;
echo "<br>";
// 构造器模式插入
public function construct_insert()
$user = "测试";
$password = "测试";
$data = array(
'user' => $user,
'password' => $password,
);
$this->db->insert('user', $data);
以上是关于codeIgniter3 学习笔记十(数据库操作)的主要内容,如果未能解决你的问题,请参考以下文章