Yii,Widgets,mysql db未选中

时间:2014-06-25 16:46:13

标签: php mysql ajax yii widget

尝试通过窗口小部件访问我们的mysql数据库,我收到此错误:数据库连接失败:未选择数据库 任何帮助是极大的赞赏! 试图通过一个小部件访问我们的mysql数据库,我收到此错误:数据库连接失败:没有选择数据库 非常感谢任何帮助!

<?php


 Yii::import('zii.widgets.CPortlet');

class Test extends CPortlet
{
    public $testVar;
    protected function renderContent()
    {
        $this->render('test');


       /* $result = mysql_query(
        "SELECT * FROM sales_rep.full_name
        ORDER BY full_name ASC");
        if(!$result) {
                die("db connection failed: ". mysql_error());
        }
        echo $this->$result;
        */
    }
}

    $connection = mysql_connect("localhost", "admin", "admin0");
    if(!$connection) {
            die("db connection failed: ". mysql_error());
    }

    $result = mysql_query(
            "SELECT * FROM sales_rep full_name
            ORDER BY full_name ASC");
            if(!$result) {
                    die("db connection failed: ". mysql_error());
            }
            echo $this->$result;


    $db_select = mysql_select_db("intranet",$connection);
    if(!db_select) {
            die("db connection failed: ". mysql_error());
    }

    $result = mysql_query("Select full_name FROM sales_rep");
    if(!$result) {
            die("db connection failed: ". mysql_error());
    }


    while ($row = mysql_fetch_array($result)) {
            echo $row[1]." ".$row[2]."<br>";
    }


    mysql_close($connection);

1 个答案:

答案 0 :(得分:1)

在Yii中,您应该在位于protected / config文件夹的main.php文件中维护数据库配置。 找到下面的部分并添加您的配置:

'components'=>array(
    [...]
    'db'=>array(
        'connectionString' => 'mysql:host=[YOUR_HOST];dbname=[YOUR_DATABASE]',
        'emulatePrepare' => true,
        'username' => 'admin',
        'password' => 'admin0',
        'charset' => 'utf8',
    ),
 [...]