MySQL problem

Hello everyone.

Can anybody help me please?

After I transfered my project to our hoster server I get this error:

CDbCommand failed to execute the SQL statement: SQLSTATE[HY000]: General error: 2030 This command is not supported in the prepared statement protocol yet.

I don’t know what to do.

Here is some information about the hoster’s server:

Apache/2.2.3 (Debian) PHP/5.2.0-8+etch16

MySQL server version: 5.0.32

PHP extension: mysqli

I use Yii 1.1.4

This is likely to be a version mis-match, would need to see the generated SQL. Try looking in your application.log file.

Here is my application.log

2010/09/21 11:16:52 [error] [system.db.CDbCommand] Error in querying SQL: SHOW CREATE TABLE shkoly

in /var/www-hosting/ippo/www/olimpiada/protected/models/Shkoly.php (27)

in /var/www-hosting/ippo/www/olimpiada/protected/controllers/SiteController.php (384)

in /var/www-hosting/ippo/www/olimpiada/index.php (12)

2010/09/21 11:16:52 [error] [exception.CDbException] exception ‘CDbException’ with message ‘CDbCommand failed to execute the SQL statement: SQLSTATE[HY000]: General error: 2030 This command is not supported in the prepared statement protocol yet’ in /var/www-hosting/ippo/www/yii/framework/db/CDbCommand.php:387

Stack trace:

#0 /var/www-hosting/ippo/www/yii/framework/db/CDbCommand.php(291): CDbCommand->queryInternal(‘fetch’, 2, Array)

#1 /var/www-hosting/ippo/www/yii/framework/db/schema/mysql/CMysqlSchema.php(194): CDbCommand->queryRow()

#2 /var/www-hosting/ippo/www/yii/framework/db/schema/mysql/CMysqlSchema.php(97): CMysqlSchema->findConstraints(Object(CMysqlTableSchema))

#3 /var/www-hosting/ippo/www/yii/framework/db/schema/CDbSchema.php(80): CMysqlSchema->createTable(‘shkoly’)

#4 /var/www-hosting/ippo/www/yii/framework/db/ar/CActiveRecord.php(2158): CDbSchema->getTable(‘shkoly’)

#5 /var/www-hosting/ippo/www/yii/framework/db/ar/CActiveRecord.php(353): CActiveRecordMetaData->__construct(Object(Shkoly))

#6 /var/www-hosting/ippo/www/olimpiada/protected/models/Shkoly.php(27): CActiveRecord::model(‘Shkoly’)

#7 /var/www-hosting/ippo/www/olimpiada/protected/controllers/SiteController.php(384): Shkoly::model()

#8 /var/www-hosting/ippo/www/yii/framework/web/actions/CInlineAction.php(50): SiteController->actionVvid()

#9 /var/www-hosting/ippo/www/yii/framework/web/CController.php(300): CInlineAction->run()

#10 /var/www-hosting/ippo/www/yii/framework/web/CController.php(278): CController->runAction(Object(CInlineAction))

#11 /var/www-hosting/ippo/www/yii/framework/web/CController.php(257): CController->runActionWithFilters(Object(CInlineAction), Array)

#12 /var/www-hosting/ippo/www/yii/framework/web/CWebApplication.php(324): CController->run(‘vvid’)

#13 /var/www-hosting/ippo/www/yii/framework/web/CWebApplication.php(121): CWebApplication->runController(‘site/vvid’)

#14 /var/www-hosting/ippo/www/yii/framework/base/CApplication.php(135): CWebApplication->processRequest()

#15 /var/www-hosting/ippo/www/olimpiada/index.php(12): CApplication->run()

#16 {main} REQUEST_URI=/olimpiada/index.php?r=site/vvid

I have to say that I use two database connections in my application.

And I noticed that this error happens only with those models that use the second connection.

Still I don’t know what I can do about it.

Please, help me.

Without seeing the generated SQL it’s hard to tell what command it’s failing on. And without knowing how you’re doing multiple connections it’s hard to say what the issue is.

One way to do a site with multiple database connections is to create a second database connection string in your config file with a base class for your models that use the alternate connection (main connection is ‘db’). So for example if we named our other connection db_alt we would create this model:


<?php

class ActiveRecordAlt extends CActiveRecord

{

    public function getDbConnection()

    {

        return yii::app()->db_alt;

    }

}

?>

Now every model that uses the second connection can use that as the base class:


class TableInDbAlt extends ActiveRecordAlt

{

// normal class code here...

}

This works fine as long as you’re not joining tables across databases, which doesn’t work. If you needed to do such a thing (like we do) then one way to work around it is to create a view or stored procedure in your database that returns the results you need from the combination of tables.

I use this class for my second connection:




<?php

abstract class AltActiveRecord extends CActiveRecord

{

    const BELONGS_TO='CBelongsToRelation';

    const HAS_ONE='CHasOneRelation';

    const HAS_MANY='CHasManyRelation';

    const MANY_MANY='CManyManyRelation';

    const STAT='CStatRelation';

 

    /**

     * @var CDbConnection the default database connection for all active record classes.

     * By default, this is the 'db' application component.

     * @see getDbConnection

     */

    public static $db;

 

    private static $_models=array();            // class name => model

 

    private $_md;                               // meta data

    private $_new=false;                        // whether this instance is new or not

    private $_attributes=array();               // attribute name => attribute value

    private $_related=array();                  // attribute name => related objects

    private $_c;                                // query criteria (used by finder only)

    private $_pk;                               // old primary key value

 

    /**

     * Returns the database connection used by active record.

     * By default, the "db" application component is used as the database connection.

     * You may override this method if you want to use a different database connection.

     * @return CDbConnection the database connection used by active record.

     */

    public function getDbConnection()

    {

        if(self::$db!==null)

            return self::$db;

        else

        {

 

self::$db=new CDbConnection(Yii::app()->db2->connectionString,Yii::app()->db2->username,Yii::app()->db2->password);

self::$db->charset='utf8';

 

 

        // Uncomment the following lines to prove that you have two database connections

        /*

            CVarDumper::dump(Yii::app()->db);

            echo '<br />';

            CVarDumper::dump(Yii::app()->db2);

            die;

        */

            if(self::$db instanceof CDbConnection)

            {

                self::$db->setActive(true);

                return self::$db;

            }

            else

                throw new CDbException(Yii::t('yii','Active Record requires a "db" CDbConnection application component.'));

        }

    }

}

?>




And it works great on my:

Apache/2.2.14 (Ubuntu)

MySQL server 5.1.41-3ubuntu12.6

PHP extension: mysql

But on our hoster’s server it shows this error.

lgoss007, I tried the code that you suggested:




<?php

class ActiveRecordAlt extends CActiveRecord

{

    public function getDbConnection()

    {

        return yii::app()->db_alt;

    }

}

?>



And it works!!! :D

Thank you very much.