查询生成器是通过数据库连接上的table((方法加载的。这为您设置了查询的FROM部分,并返回查询生成器类的一个新实例:
我正在使用CodeIgniter 4。首先,我写这篇文章是为了从数据库中获取记录,但这向我显示了一个错误(在null上调用成员函数table()
(
$CI = & get_instance();
$CI -> db -> select('*');
$CI -> db -> from($table_name);
然后我阅读了文档并写下了这个
$db->table("tablename");
但这种方法也失败了。
$db = ConfigDatabase::connect();
$builder = $db->table('users');
//loading query builder
$output = $builder->table('table_name')
->get();
// Produces: SELECT * FROM table_name
若要获得数组形式的结果,您可以再添加一行代码。
$output = $output->getResultArray();
用于选择特定的文件。
$db = ConfigDatabase::connect();
$builder = $db->table('users');
//loading query builder
$output = $builder->table('table_name')
->select('filedname2, fieldname2, fieldname3,..')
->get();
$output = $output->getResultArray();
您可以使用where子句。有关更多详细信息,请参阅codeigner4文档页面。https://codeigniter4.github.io/userguide/database/query_builder.html#looking-针对特定数据
//initialize database connection
$db = ConfigDatabase::connect();
//created builder to get queries results
$builder = $db->table('users');
//select particular columns
$builder->select('id','first_name','last_name');
//if you want to select all columns
$builder->select('*')
//if you want to put where
$builder->where('id',1);
//if you want to put order by desc or asc
$builder->orderBy('id desc');
//get records
$output = $builder->get();
//if you want to return your results in array format then,
return $output->getResultArray();
//if you want to return your results in std object then,
return $output->getResult();