Handy MySQL Commands For PHP Advancement

Posted in WordPress on Jul 19, 2014

When I’m producing PHP purposes I generally have a DOS window open so I can run SQL scripts from the command line. Usually, this is so I can look at that MySQL database tables are remaining updated appropriately when I am operating PHP scripts.

There are not that numerous command line responsibilities I need to have to accomplish, but it is valuable to be capable to operate them as and when I want to. In this tutorial I am going to glimpse at a couple of MySQL instructions I locate really valuable through the advancement of PHP applications.

Logging on to the MySQL keep track of

To log on to the MySQL monitor:
Open a command prompt window, and navigate to the the mysqlbin folder.

Run the command:



mysql -u person -p

If prompted, enter the password for the databases person.

Setting a root password

If you have not bought a root password established, right here is how you set one particular.
Log on to the MySQL monitor as described previously mentioned.

Operate the command:

established password for root@localhost=password(‘password’)

Updating a table

I pretty normally need to include or delete new columns, or modify present kinds.

To perform the subsequent instructions you need to have to be logged on to the MySQL keep track of, as explained higher than.

Including a new column

The structure is:

Change table tablename

Insert new_column variety

After column

And here are a pair of illustrations []).force()

Change table buyers

Incorporate surname char(thirty)

Immediately after firstname

Alter desk buyers

Include age int (two) NOT NULL default ‘0’

After surname

Modifying an present column

Occasionally it is needed to update existing columns listed here is the format:Alter desk tablename

MODIFY column style

And this is an instance:

Change table consumers

MODIFY age int (three) NOT NULL default ‘0’

Dropping a column

Here’s the format:Change table tablename

Fall column

And here’s an example:

Change desk buyers

Drop age

Dumping the contents of a databases into an SQL file

For backup uses, if nothing at all else, it is from time to time useful to dump all the contents of a database into an SQL file. Here is how you do it:
Open a command prompt window, and navigate to the the mysqlbin folder.

Operate the command:

mysqldump -u databases_consumer_identify -p databases_title &gt database_title.sql

When prompted, enter the password for the databases consumer.

Sending the output from an SQL query to a file

Occasionally it is valuable to send the output from a query to a file, for illustration, a text file. Listed here is an example of how to do it.
Log onto the MySQL keep track of.

Run the SQL question, for example:

Decide on identify,surname FROM buyers into outfile ‘/tmp/people.txt’

By John Dixon

Leave a Reply

Your email address will not be published. Required fields are marked *