使用框架无法向MySQL数据库插入数据,疑连接有误
本帖最后由 KevinDean 于 2014-12-16 15:06 编辑模型的插入函数为
$data = array(
'filename' => $this->input->post('filename'),
'filepath' => './tmp/'.$this->input->post('filename'),
'filetype' => $this->input->post('filetype'),
'filetime' => date('Y-m-d', time()),
'fileowner'=> $this->input->post('fileowner'),
'filegroup'=> $this->input->post('group'),
'other' => $this->input->post('filename'),
);
$this->db->insert('filedata', $data);
发现无法插入函数,于是自己写了个SQL插入语句,
$filename=$data['filename'];
$filepath=$data['filepath'];
$filetype=$data['filetype'];
$filetime=date('Y-m-d', time());
$fileowner =$data['fileowner'];
$filegroup =$data['filegroup'];
$other =$data['other'];
print_r( $query = $this->db->get('filedata'));
$db=mysqli_connect('localhost','root','');
mysqli_select_db($db,"biocommunity");
$sql="INSERT INTO filedata (filename,filepath, filetype,filetime,fileowner,filegroup,other) VALUES ('$filename','$filepath','$filetype','$filetime','$fileowner','$filegroup','$other')";
print_r($sql);
echo $sql;
echo $result=mysqli_query($db,$sql);
mysqli_close($db);
运行结果成功插入了。
于是怀疑连接存在问题,尝试了一下查询语句
print_r( $query = $this->db->get('filedata'));
输出为
1CI_DB_mysql_result Object
(
=>
=>
=> Array
(
)
=> Array
(
)
=> Array
(
)
=> 0
=>
=>
)
这让我感到非常的困惑不解。
再仔细查看了application/config/database.php 中的配置文件,发现配置确实是对的。
开发环境为win32下的xampp,PHP/5.5.15,
[*]服务器类型: MySQL
[*]服务器版本: 5.6.20 - MySQL Community Server (GPL
请教一下大家,我这种情况问题到底是出在哪儿 !非常感谢!!!
Closer 发表于 2014-12-16 09:28
你的 database 有載入嗎?
一般我都是在 config\autoload.php 自動載入
$autoload['libraries'] = array('d ...
您好!
1.database里面已经自动载入了!
2.按照您的说法,我设置$db['default']['db_debug'] = TRUE;之后,返回了错误
A Database Error OccurredUnable to connect to your database server using the provided settings.Filename: D:\Program Files (x86)\xampp\htdocs\biocommunity\system\database\DB_driver.phpLine Number: 124
我找到第124行,发现是
$this->conn_id = ($this->pconnect == FALSE) ? $this->db_connect() : $this->db_pconnect();
// No connection resource?Throw an error
if ( ! $this->conn_id)
{
log_message('error', 'Unable to connect to the database');
if ($this->db_debug)
{
$this->display_error('db_unable_to_connect'); //124行代码
}
return FALSE;
}
此外 我提供的设置是
$active_group = 'default';
$active_record = TRUE;
$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'root';
$db['default']['password'] = 'liukai';
$db['default']['database'] = 'biocommunity';
$db['default']['dbdriver'] = 'mysql';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = '';
$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_general_ci';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;
$db['default']['port'] = 5432;
谢谢您的回答!!!
首先感谢您的回答!!!
我想得到上传文件的时间所以'filetime' => date('Y-m-d', time())
$filename=$data['filename'];
$filepath=$data['filepath'];
$filetype=$data['filetype'];
$filetime=date('Y-m-d', time());
$fileowner =$data['fileowner'];
$filegroup =$data['filegroup'];
$other =$data['other'];
$sql="INSERT INTO filedata (filename,filepath, filetype,filetime,fileowner,filegroup,other) VALUES ('$filename','$filepath','$filetype','$filetime','$fileowner','$filegroup','$other')";
$query = $this->db->query("$sql");
//print_r($query);
echo $query->num_rows();
得到错误 Fatal error: Call to a member function num_rows() on a non-object
database.php 的配置为
$active_group = 'default';
$active_record = TRUE;
$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'root';
$db['default']['password'] = '';
$db['default']['database'] = 'biocommunity';
$db['default']['dbdriver'] = 'mysql';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = FALSE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = '';
$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_general_ci';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;
$db['default']['port'] = 5432;
aqxinzhuan 发表于 2014-12-15 19:24
建议把错误信息贴出来瞧瞧。
另外,使用echo $this->db->last_query();看看执行的SQL语句有没有问题。 ...
按照您说的,我重新写了以下语句
$data = array(
'filename' => $this->input->post('filename'),
'filepath' => './tmp/'.$this->input->post('filename'),
'filetype' => $this->input->post('filetype'),
'filetime' => date('Y-m-d', time()),
'fileowner'=> $this->input->post('fileowner'),
'filegroup'=> $this->input->post('group'),
'other' => $this->input->post('filename')
);
$query=$this->db->insert('filedata', $data);
echo $this->db->last_query();
输出结果为
INSERT INTO `filedata` (`filename`, `filepath`, `filetype`, `filetime`, `fileowner`, `filegroup`, `other`) VALUES ('12152002', './tmp/12152002', 'txt', '2014-12-15', 'liukai', 'group', '12152002')
发现表名和字段的单引号似乎不对,不知道什么问题,希望您能解惑!!!
本帖最后由 Closer 于 2014-12-15 16:58 编辑
$data = array(
'filename' => $this->input->post('filename'),
'filepath' => './tmp/'.$this->input->post('filename'),
'filetype' => $this->input->post('filetype'),
'filetime' => date('Y-m-d', time()), //你的用意是?
'fileowner'=> $this->input->post('fileowner'),
'filegroup'=> $this->input->post('group'),
'other' => $this->input->post('filename'), //多了一個 ,
);
$this->db->insert('filedata', $data);
去除那個點試試
輸出結果看看
echo $query->row_array();
static/image/hrline/line3.png
貼上你的 database.php 的配置
也許是那邊有問題
KevinDean 发表于 2014-12-15 17:35
首先感谢您的回答!!!
我想得到上传文件的时间所以'filetime' => date('Y-m-d', time())
$query = $this->db->query("$sql");//双引号要去掉
建议把错误信息贴出来瞧瞧。
另外,使用echo $this->db->last_query();看看执行的SQL语句有没有问题。 首先感谢您的回答!!!
1.双引号去掉之后还是不行
2.我的问题就在于 任何与数据库的交互都没有反应,无法添加也无法查询数据。比如我执行
$this->db->insert('filedata', $data);
之后 没有一丝改变, 查询操作返回也是为空 。3.没有任何报错信息。
这让我感到无比的沮丧。。。。
KevinDean 发表于 2014-12-15 20:05
按照您说的,我重新写了以下语句
把这个SQL贴到phpmyadmin里运行下试试看有没有错,还有那不叫单引号叫反引号,看着是没错的
本帖最后由 Closer 于 2014-12-16 09:33 编辑
你的 database 有載入嗎?
一般我都是在 config\autoload.php 自動載入
$autoload['libraries'] = array('database');
你把 $db['default']['db_debug'] = TURE; 打開
看看載入失敗的原因
一叶扁舟 发表于 2014-12-16 09:02
把这个SQL贴到phpmyadmin里运行下试试看有没有错,还有那不叫单引号叫反引号,看着是没错的
...
您好!
将SQL语句粘贴到phpMyAdmin中运行成功了,成功的插入了!
页:
[1]
2