JDT |
John Dixon |
Essential MySQL for PHP Development |
||||
|
When I'm developing PHP applications I normally have a DOS window open so I can run SQL scripts from the command line. Often, this is so I can check that MySQL database tables are being updated correctly while I am running PHP scripts. There aren't that many command line tasks I need to perform, but it is useful to be able to run them as and when I need to. In this tutorial I'll look at a few MySQL commands I find really useful during the development of PHP applications. Logging onto the MySQL monitorTo log onto the MySQL monitor: 1. Open a command prompt window, and navigate to the the mysql\bin folder. 2. Run the command: mysql OR mysql -u user -p If prompted, enter the password for the database user. Setting a root passwordIf you haven't got a root password set, here's how you set one. 1. Log onto the MySQL monitor as described above. 2. Run the command: set password for root@localhost=password('password'); Updating a tableI quite often need to add or delete new columns, or modify existing ones. To perform the following commands you need to be logged onto the MySQL monitor, as described above. Adding a new column The format is: ALTER table tablename And here are a couple of examples: ALTER table users ALTER table users Modifying an existing column Sometimes it is necessary to update existing columns; here's the format: ALTER table tablenameMODIFY column type; And here's an example: ALTER table users Dropping a column Here's the format: ALTER table tablenameDROP column; And here's an example: ALTER table users Dumping the contents of a database into an SQL fileFor backup purposes, if nothing else, it's sometimes useful to dump all the contents of a database into an SQL file. Here's how you do it: 1. Open a command prompt window, and navigate to the the mysql\bin folder. 2. Run the command: mysqldump -u database_user_name -p database_name > database_name.sql When prompted, enter the password for the database user. Sending the output from an SQL query to a fileSometimes it is useful to send the output from a query to a file, for example, a text file. Here's an example of how to do it. 1. Log onto the MySQL monitor. 2. Run the SQL query, for example: SELECT name,surname FROM users into outfile '/tmp/users.txt'; Author: John Dixon Go back to MySQL Tutorials home page Go back to Tutorials home page
|
|
|
|||||
|
© 2007-2009 - John Dixon Technology Ltd |
|||||