how to custom connect the database from yii - php

Im trying to connect the db in yii connection how can i do that ?
In below screen shot there is an cron.php i have trying to connect the database from root folder how to d that ?
Cron.php
require('framework/db/CDbConnection.php');
$connection=new CDbConnection($dsn,'root','');
$connection->active=true;
I need to use Yii connection instead of mysql
$mysql_con = mysql_connect('localhost','root','');
mysql_select_db('som',$mysql_con); //local DB

If you want to use cron for some tasks, consider creating yii console application commands. Configure it to use your db settings and call tasks that you need like that yiic <command-name> <action-name>

Related

cache/flush-schema command for specific database connection in yii2-advanced

I'm running the yii cache/flush-schema command on the root of yii-advanced project but it's working on only one and default db connection. Whereas, the application is having multiple database connections and I need to run the command on specific database.
Running the command with specifying the database connection name in it, would do the job, as:
yii cache/flush-schema db_connection_name

SQlite and PHP - Console issue

On Windows Server 2008 R2, I am trying to set up a scheduled task to automatically run a PHP script which then adjusts the SQlite database. I am using SQlite as I need to use a database file. Whenever I run the script, it returns this error:
But when I run it in the web browser, it works perfectly.
My code is:
$db = new PDO("sqlite:server_status.db");
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "SELECT * FROM stats";
$results = $db->query($sql);
foreach($results as $row) {
print_r($row);
}
I'm no expert with PDO or SQLITE but looking at your console output it seems that your current working directory is c:/windows/system32 but that is not your web root.
Therefore PDO will be looking for your database file at: c:/windows/system32/server_status.db which i presume is not the correct path.
Either define your sqlite database location as a full absolute path starting with C:\, or change your current directory to the folder containing the sqlite database file.

When I use c9.io ,How can I connect to MySQL

c9.io is a verygood website
I have a php zoon , and when I want to connect to MySQL,I don't know password.
I have try [space] root ...
but,all is wrong.
I can open mysql in shell , no password ,my operate return error:
error: 'Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)'
It's looks like, mysqld is not started 。(I have try “mysqld start”,but faild)
What I want to know,
Is Mysql service free on c9.io ?
We are extremely happy to announce our first iteration of MySQL support in Cloud9. It makes it super easy to install, start and stop a MySQL instance right in your workspace. The nice thing is that every workspace will run a separate database so your projects will never interfere with each other. You can control MySQL with the mysql-ctl command line tool run from the terminal.
# start MySQL. Will create an empty database on first start
$ mysql-ctl start
# stop MySQL
$ mysql-ctl stop
# run the MySQL interactive shell
$ mysql-ctl cli
You can then connect to the database with following parameters:
Option Value Comment
Hostname $IP The same local IP as the application you run on Cloud9
Port 3306 The default MySQL port number
User $C9_USER Your Cloud9 user name
Password - No password since you can only access the DB from within the workspace
Database c9 The database name
Of course this is just the beginning. We have for example plans to add a management UI to start and stop databases or pre-installing tools like phpMyAdmin. However we don’t want to make you wait for the fully integrated feature while we already have something that is enabling a lot of use cases and still super easy to use.
Stay tuned and happy coding.
The documentation show how start, stop, and run the mysql environment.
Start the MySQL shell mysql-ctl start then in yor file.php:
$ip = getenv("REMOTE_ADDR");
$port = "3306";
$user = "YorUsername";
$DB = "c9";
$conn = mysql_connect('$ip', '$user', '', '$db', '$port')or die(mysql_error());
mysql_select_db('$db','$conn')or die(mysql_error());
mysql_query("select * from YourTableName",'$conn')or die(mysql_error());
The line getenv("REMOTE_ADDR") return the same local IP as the application you run on Cloud9.

Can I move MySQL database from Server A to Server B if all I have is FTP and MySQL user/pass for Server A?

A friend of mine wants to move his website to my Slice Host slice. The site uses a MySQL database. He gave me FTP login info, but nothing else. I have called and emailed his hosting company hoping that they would be able to give me access to PHPMyAdmin if it is installed. I have not received a response from them. Neither has my friend.
I can find the database username, password, name, etc. in the PHP files via FTP.
I tried uploading and running (in a browser) this PHP file:
$con = mysql_connect("mysql.address.com","user","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("databasename", $con);
system("mysqldump -uuser -ppassword databasename > dump.sql");
?>
...but it did not create a file named dump.sql on the server.
Any ideas on how I can accomplish my goal?
Ultimately, mysqldump isn't special. It communicates with the MySQL server just like any other client. You could write PHP code to dump each table to e.g., xml and then more code to reload them all. For most web apps, the database will just contain base tables, so you'll really only need:
SHOW DATABASES
SHOW TABLES
The above two will tell you all the tables you need to dump. You could also do a select from information_schema.tables.
SHOW CREATE TABLE «foo»
This will give you the SQL to recreate the table.
SELECT * FROM «foo»
This will (obviously) give you the data.
I'm guessing instead of writing this, you can probably find pre-existing code. phpMyAdmin has it, I believe, but that's quite a bit of complexity for just doing a quick dump...
If you have FTP and really want phpMyAdmin, why don't you install it then?
You could use mysql GUI tools using the username and password you found, but this may only work from the server (or a file on the server).
Instead of calling a system() function which you won't have access to (neither will you have mysqldump in most cases on shared hosting), use the "echo" command on an sql query, so:
$myQuery = mysql_query("show tables");
while($row = mysql_fetch_row($myQuery))
{
echo $row[0];
}
You could also test whether the ftp username/pass gives you shell access (ssh) and try running your command from the command line as suggested in the comments
I should add its been a while since I've done php, so my code might not be quite right but you get the idea :)

Why Can't i build my database in symfony using propel orm.?

I am new to the php framework symfony.I have a problem creating the database.I am using propel orm. As usual I made necessary changes in the database.yml page then I edited the schema.yml page. and in order to build the database in the command prompt typed the command..
c:>php propel:build-all
and i am getting the error message as
"could not open input
file:propel:build-all"
Before propel build all in symfony 1.4 . Make an edit in MysqlDDLBuilder.php as mentioned in
link
then Enter project directory in command prompt
and type as below example in my windows system:
C:\wamp\bin\php\php5.3.5\php.exe
symfony propel:build-all
Check this link on how to create the sql using propel:
Building The Database
to create the sql use : // will create a sqlfile.sql
propel-gen sql
then create your database using mysqladmin
mysqladmin -u root -p create my_db_name
import the sql file
mysql -u root -p my_db_name < sqlfile.sql

Categories