After setting up the your MySQL connection in your web application (see my previous post), you should ensure that your database connection is actually working especially for new server setups. Let me introduce you to the interactive shell for your application and ensure that there are no errors when you attempt to reference the application db component.
Using the interactive shell
As you would remember, we used the command line utility called yiic to create a new web application in Yii. Another command you can use with yiic is called shell. This will allow you to run PHP commands within the context of the Yii application, straight from the command line.
To start the shell, go to the root directory of your application, that is the directory where you can located index.php entry script. Then run the yiic utility and passing in shell as the command. See the screenshot below
This will allow you to enter the commands directly after the >> prompt.
You would want to test your db connection to ensure that your MySQL Database is accessible from your web application. You can simply echo out the connection string and verify that it returns what you have set in the MySQL configuration (see Connect to a MySQL Database). So from the shell prompt, just type in:
>> echo Yii::app()->db->connectionString;
It should return something similar to the following:
mysql:host=localhost;dbname=<db-name>
This will show that the db application component is configured correctly and available for use in your web applicaiton.
*Note: To exit from shell, just type exit from the prompt.
Using the interactive shell
As you would remember, we used the command line utility called yiic to create a new web application in Yii. Another command you can use with yiic is called shell. This will allow you to run PHP commands within the context of the Yii application, straight from the command line.
To start the shell, go to the root directory of your application, that is the directory where you can located index.php entry script. Then run the yiic utility and passing in shell as the command. See the screenshot below
This will allow you to enter the commands directly after the >> prompt.
You would want to test your db connection to ensure that your MySQL Database is accessible from your web application. You can simply echo out the connection string and verify that it returns what you have set in the MySQL configuration (see Connect to a MySQL Database). So from the shell prompt, just type in:
>> echo Yii::app()->db->connectionString;
It should return something similar to the following:
mysql:host=localhost;dbname=<db-name>
This will show that the db application component is configured correctly and available for use in your web applicaiton.
*Note: To exit from shell, just type exit from the prompt.

No comments:
Post a Comment