代码之家  ›  专栏  ›  技术社区  ›  NinjaCat

连接到数据库并执行查询

  •  0
  • NinjaCat  · 技术社区  · 16 年前

    我本来想在他们的论坛上发布这个,但他们现在好像不在了。

    $mydb  = Database::instance('mydb');
    $query = DB::select()->from('codes')->where('name', '=', 'PHP'); 
    

    1) 在使用我在中定义的自定义数据库实例时,我看不出这一点数据库.php. 它总是使用默认值。我怎么知道?

    mysql_connect(): Access denied for user ''@'localhost' (using password: NO) ~ MODPATH\database\classes\kohana\database\mysql.php [ 56 ]
    

    2) 我有点困惑,因为根据他们的教程,execute($mydb)应该不是必需的。。。但我看到一些帖子暗示这是真的。是哪一个?

    配置文件:

    <?php defined('SYSPATH') or die('No direct access allowed.');
    
    return array
    (
        'default' => array
        (
            'type'       => 'mysql',
            'connection' => array(
                /**
                 * The following options are available for MySQL:
                 *
                 * string   hostname     server hostname, or socket
                 * string   database     database name
                 * string   username     database username
                 * string   password     database password
                 * boolean  persistent   use persistent connections?
                 *
                 * Ports and sockets may be appended to the hostname.
                 */
                'hostname'   => 'localhost',
                'database'   => 'kohana',
                'username'   => FALSE,
                'password'   => FALSE,
                'persistent' => FALSE,
            ),
            'table_prefix' => '',
            'charset'      => 'utf8',
            'caching'      => FALSE,
            'profiling'    => TRUE,
        ),
        'alternate' => array(
            'type'       => 'pdo',
            'connection' => array(
                /**
                 * The following options are available for PDO:
                 *
                 * string   dsn         Data Source Name
                 * string   username    database username
                 * string   password    database password
                 * boolean  persistent  use persistent connections?
                 */
                'dsn'        => 'mysql:host=localhost;dbname=kohana',
                'username'   => 'root',
                'password'   => 'r00tdb',
                'persistent' => FALSE,
            ),
            /**
             * The following extra options are available for PDO:
             *
             * string   identifier  set the escaping identifier
             */
            'table_prefix' => '',
            'charset'      => 'utf8',
            'caching'      => FALSE,
            'profiling'    => TRUE,
        ),
        'mydb' => array(
            'type'       => 'mysql',
            'connection' => array(
                'hostname'   => '127.0.0.1',
                'username'   => 'test',
                'password'   => 'test',
                'persistent' => FALSE,
                'database'   => 'mydb',
            ),
            'table_prefix' => '',
            'charset'      => 'utf8',
            'profiling'    => TRUE,
        ),
    

    1 回复  |  直到 16 年前
        1
  •  1
  •   The Pixel Developer    16 年前

    数据库::实例('mydb');

    这不是数据库名称,而是数据库配置组。查看数据库配置文件以了解其结构。

    你能上传你的配置文件吗?

    推荐文章