我创建了一个库,并尝试通过codeigniter访问数据库。
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
班级历史{
function __construct() {
//$CI->load->database();
//$this->load->library('database');
$CI =& get_instance();
$CI->load->database();
}
public function create_history($id){
//$this->load->database();
$sql = "INSERT INTO inventory_history SELECT null,i.* FROM inventory i where i.inventory_id = :id";
$query = $this->CI->db->conn_id->prepare($sql);
$query->bindParam(':id', $id, PDO::PARAM_INT);
return $query->execute();
}}
但是我在插入查询附近遇到错误。通过谷歌,我找到了一个告诉使用$this->ci->db
执行查询的人。我无法指出数据库执行中出现错误的原因。如何在codeigniter中将数据库函数调用到我的自定义库中?
答案 0 :(得分:13)
class YourLibName {
private $CI;
function __construct() {
$this->CI =& get_instance();
$this->CI->load->database();
}
}
更新:我认为您不应该在库中加载和使用数据库对象。您最好创建模型并从库中调用它。库应该是独立的,可以与其他应用程序一起使用。
答案 1 :(得分:2)
在application / config / autoload.php中添加:
$autoload['libraries'] = array('database'); // add database in array
您图书馆的相同代码:
Class CutomLib
{
function getData(){
$CI =& get_instance();
$query = $CI->db->get('data');
return $query->result();
}
}
答案 2 :(得分:1)
function __construct() {
//$CI->load->database();
//$this->load->library('database');
$CI =& get_instance();
$CI->load->database();
}
应该是
function __construct() {
//$CI->load->database();
//$this->load->library('database');
$this->CI =& get_instance();
$this->CI->load->database();
}
答案 3 :(得分:1)
//您的课程 公共功能__construct() {
$this->CI =& get_instance();
$this->CI->load->database('default',TRUE);
}
//如何在函数内部 公共功能funcName(){
$query = $this->CI->db->query("your QUERY");
$result = $query->result();
//what ever you want to do then
}
希望获得帮助
答案 4 :(得分:0)
好的,这是我如何设法从用户端创建自定义数据库而不触及核心文件:
首先我制作表单以获取用户的所有详细信息(更像是像wordpress一样安装我们的应用程序)。
<?php echo form_open('your_controller_path'); ?>
<input class="form-control m-input" type="text" name="dbName" value="">
<span class="help-block"><small><?php echo form_error('dbName'); ?>
</small></span>
<input class="form-control m-input" type="text" name="dbUsername"
value="">
<span class="help-block"><small><?php echo form_error('dbUsername'); ?>
</small></span>
<input class="form-control m-input" type="text" name="dbPassword" value="">
<span class="help-block"><small><?php echo form_error('dbPassword'); ?>
</small></span>
<input class="form-control m-input" type="text" name="dbHostname" value="">
<span class="help-block"><small><?php echo form_error('dbHostname'); ?></small></span>
<input class="form-control m-input" type="text" name="dbPrefix" value="">
<span class="help-block"><small><?php echo form_error('dbPrefix'); ?>
</small></span>
<button type="submit" class="btn btn-sm btn-success">
Submit
</button>
<?php echo form_close(); ?>
我从控制器获取详细信息并覆盖默认dsn,然后在构造函数中加载它们: -
$ this-&gt; load-&gt; dbutil();
if($this->dbutil->database_exists($this->input->post('dbName')))
{
echo $this->input->post('dbName')." Database Already Exist";
}
else
{
$this->db->hostname = $this->input->post('dbHostname');
$this->db->database = $this->input->post('dbName');
$this->db->username = $this->input->post('dbUsername');
$this->db->password = $this->input->post('dbPassword');
$this->db->dbprefix = $this->input->post('dbPrefix');
$dsn = 'dbdriver://'. $this->db->username.':'.$this->db->password.'@'.$this->db->hostname.'/'.$this->db->database;
if(!$this->load->database($dsn))
{
if ($this->dbforge->create_database($this->input->post('dbName')))
{
echo 'Database created!';
$this->db->db_select($this->input->post('dbName'));
}
}else
{
echo "Sorry Connection Problem : ".$dsn ;
}
}
我希望它有所帮助