用户
 找回密码
 入住 CI 中国社区
搜索
查看: 4507|回复: 16
收起左侧

[已解决] 使用框架无法向MySQL数据库插入数据,疑连接有误

[复制链接]
发表于 2014-12-15 16:28:13 | 显示全部楼层 |阅读模式
本帖最后由 KevinDean 于 2014-12-16 15:06 编辑

模型的插入函数为


PHP复制代码
$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插入语句,
PHP复制代码
 
$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
(
    [conn_id] =>
    [result_id] =>
    [result_array] => Array
        (
        )

    [result_object] => Array
        (
        )

    [custom_result_object] => Array
        (
        )

    [current_row] => 0
    [num_rows] =>
    [row_data] =>
)
这让我感到非常的困惑不解。
再仔细查看了application/config/database.php 中的配置文件,发现配置确实是对的。

开发环境为win32下的xampp,PHP/5.5.15,
  • 服务器类型: MySQL
  • 服务器版本: 5.6.20 - MySQL Community Server (GPL
请教一下大家,我这种情况问题到底是出在哪儿 !非常感谢!!!

 楼主| 发表于 2014-12-16 12:40:15 | 显示全部楼层
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 Occurred

Unable to connect to your database server using the provided settings.

Filename: D:\Program Files (x86)\xampp\htdocs\biocommunity\system\database\DB_driver.php

Line Number: 124


我找到第124行,发现是
       

PHP复制代码
        $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;
                }
 
复制代码
此外 我提供的设置是

  1. $active_group = 'default';
  2. $active_record = TRUE;

  3. $db['default']['hostname'] = 'localhost';
  4. $db['default']['username'] = 'root';
  5. $db['default']['password'] = 'liukai';
  6. $db['default']['database'] = 'biocommunity';
  7. $db['default']['dbdriver'] = 'mysql';
  8. $db['default']['dbprefix'] = '';
  9. $db['default']['pconnect'] = TRUE;
  10. $db['default']['db_debug'] = TRUE;
  11. $db['default']['cache_on'] = FALSE;
  12. $db['default']['cachedir'] = '';
  13. $db['default']['char_set'] = 'utf8';
  14. $db['default']['dbcollat'] = 'utf8_general_ci';
  15. $db['default']['swap_pre'] = '';
  16. $db['default']['autoinit'] = TRUE;
  17. $db['default']['stricton'] = FALSE;

  18. $db['default']['port'] = 5432;
复制代码

谢谢您的回答!!!



 楼主| 发表于 2014-12-15 17:35:54 | 显示全部楼层
首先感谢您的回答!!!

我想得到上传文件的时间  所以  'filetime' => date('Y-m-d', time())   
PHP复制代码
 
            $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;
 楼主| 发表于 2014-12-15 20:05:34 | 显示全部楼层
aqxinzhuan 发表于 2014-12-15 19:24
建议把错误信息贴出来瞧瞧。
另外,使用echo $this->db->last_query();看看执行的SQL语句有没有问题。 ...

按照您说的,我重新写了以下语句
PHP复制代码
$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')

发现表名和字段的单引号似乎不对,不知道什么问题,希望您能解惑!!!
发表于 2014-12-15 16:45:56 | 显示全部楼层
本帖最后由 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();



貼上你的 database.php 的配置
也許是那邊有問題

 
发表于 2014-12-15 19:17:20 | 显示全部楼层
KevinDean 发表于 2014-12-15 17:35
首先感谢您的回答!!!

我想得到上传文件的时间  所以  'filetime' => date('Y-m-d', time())   

$query = $this->db->query("$sql");//双引号要去掉
发表于 2014-12-15 19:24:35 | 显示全部楼层
建议把错误信息贴出来瞧瞧。
另外,使用echo $this->db->last_query();看看执行的SQL语句有没有问题。
 楼主| 发表于 2014-12-15 19:56:52 | 显示全部楼层
首先感谢您的回答!!!

1.双引号去掉之后还是不行
2.我的问题就在于 任何与数据库的交互都没有反应,无法添加也无法查询数据。比如我执行
$this->db->insert('filedata', $data);
之后 没有一丝改变, 查询操作  返回也是为空 。3.没有任何报错信息。
这让我感到无比的沮丧。。。。
发表于 2014-12-16 09:02:57 | 显示全部楼层
KevinDean 发表于 2014-12-15 20:05
按照您说的,我重新写了以下语句

把这个SQL贴到phpmyadmin里运行下试试看有没有错,还有那不叫单引号叫反引号,看着是没错的
发表于 2014-12-16 09:28:15 | 显示全部楼层
本帖最后由 Closer 于 2014-12-16 09:33 编辑

你的 database 有載入嗎?
一般我都是在 config\autoload.php 自動載入
$autoload['libraries'] = array('database');

你把 $db['default']['db_debug'] = TURE; 打開
看看載入失敗的原因
 


 楼主| 发表于 2014-12-16 12:35:18 | 显示全部楼层
一叶扁舟 发表于 2014-12-16 09:02
把这个SQL贴到phpmyadmin里运行下试试看有没有错,还有那不叫单引号叫反引号,看着是没错的
...

您好!
将SQL语句粘贴到phpMyAdmin中运行成功了,成功的插入了!

本版积分规则