Useful MySQL Commands For PHP Development
Useful MySQL Commands 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 are not 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 monitor
To log onto the MySQL monitor:
Open a command prompt window, and navigate to the the mysql\bin folder.
Run the command:
mysql
OR
mysql -u user -p
If prompted, enter the password for the database user.
Setting a root password
If you have not got a root password set, here is how you set one.
Log onto the MySQL monitor as described above.
Run the command:
set password for root@localhost=password(’password’);
Updating a table
I 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
ADD new_column type
AFTER column;
And here are a couple of examples:
ALTER table users
ADD surname char(30)
AFTER firstname;
ALTER table users
ADD age int (2) NOT NULL default ‘0′
AFTER surname;
Modifying an existing column
Sometimes it is necessary to update existing columns; here is the format:
ALTER table tablename
MODIFY column type;
And here’s an example:
ALTER table users
MODIFY age int (3) NOT NULL default ‘0′;
Dropping a column
Here’s the format:
ALTER table tablename
DROP column;
And here’s an example:
ALTER table users
DROP age;
Dumping the contents of a database into an SQL file
For 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:
Open a command prompt window, and navigate to the the mysql\bin folder.
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 file
Sometimes it is useful to send the output from a query to a file, for example, a text file. Here is an example of how to do it.
Log onto the MySQL monitor.
Run the SQL query, for example:
SELECT name,surname FROM users into outfile ‘/tmp/users.txt’;
About the Author: John Dixon is a web developer working through his own company John Dixon Technology Limited. The company also develops and supplies a free accounting bookkeeping software tool called Earnings Tracker. The company’s web site contains various articles, tutorials, news feeds, and a finance and business blog.
Apache, MySQL & PHP for Windows
Apache, MysQL and PHP for Windows could be a nice nice thing to have on your Windows workstation. You could try and experiment with all kinds of nice PHP and MySQL based applications right on your Windows desktop running Apache, instead of having to access a full-featured server.
Most people have Windows as their workstation and it can be sometimes difficult to switch to another operating system. So, you may have always wanted to run PHP applications on your Windows machine but wondered if it is too difficult to install or if the hassle will be worth it.
This article gives you the essential information to get started right away. Even if you are a seasoned PHP, MySQL and Apache guru, the checklist below will still be helpful in your installation process.
There are lots of 3rd party software that bundles Apache, MySQL & PHP in one package and installs them on our computer. We do not recommend this and suggest that you directly get Apache, MySQL & PHP from their official sites.
Apache
1. Get Apache 1.3.33 from here: http://httpd.apache.org/download.cgi.
2. Choose a mirror close to you and in the same page, look for the Win32 Binary (Self extracting) file: apache_1.3.33-win32-x86-no_src.exe.
3. Download the file and save it on your hard disk. Run the installer and the self- extracting wizard will guide you through the rest of the steps. Choose all the default settings and run Apache as a service.
4. Remember to put “localhost” when asked for a Server name/Domain name. Use “administrator@localhost” when asked for the administrative email account.
5. Now point your browser to: http://localhost and you should see an Apache Test Page.
6. You can change this page by creating an “index.html” page here “C:Program FilesApache GroupApachehtdocs”.
7. You can manually start and stop the Apache server. In a Windows command prompt, type “net stop apache” or “net start apache”.
MySQL
1. Get MySQL 4.1.7 from here: http://dev.mysql.com/downloads/mysql/4.1.html
2. Under the Windows downloads section, choose Windows Essentials (x86) and click on the Pick a Mirror link.
3. Download the file mysql-4.1.7-essential-win.msi and save it on your hard disk. Run the installer and the self-extracting wizard will guide you through the rest of the steps. Remember the root password when prompted for it in the installation process.
4. Once the installation is done, on your Windows toolbar, go to “Start->Programs- >MySQL->MySQL Server 4.1->MySQL Command Line Client”.
5. Type the root password and you should be logged in to the MySQL shell.
6. Type “show databases;” to see the list of databases. Type “quit” when you are done.
PHP
1. Get PHP 4.3.10 from here: http://www.php.net/downloads.php
2. Under the Windows Binaries section, choose the file: PHP 4.3.10 zip package size 7,405Kb dated 15 Dec 2004.
3. Download the file and save it on your hard disk. Unzip the file and rename the extracted folder to “php”. Now move this folder “php” and place it under “C: Program Files”.
4. Move all the files under “C:Program Filesphpdlls” and “C:Program Filesphpsapi” to here: “C:Program Filesphp”.
5. Copy the file php.ini-recommended to “C:WINDOWS” and rename it to php.ini
6. Edit your Apache “httpd.conf” configuration file located here: “C:Program FilesApache GroupApacheconf”.
7. Add the following lines in httpd.conf:
LoadModule php4_module “C:/Program Files/php/php4apache.dll”
AddModule mod_php4.c
AddType application/x-httpd-php .php
8. Now stop your server by issuing the following command in Windows command prompt: “net stop apache”. Then type “net start apache” to start your server. We are now going to test the PHP installation.
9. Go to “C:Program FilesApache GroupApachehtdocs” and create a file test.php
10. Edit test.php and add the following code:
php
phpinfo();
?>
11. Point your browser to http://localhost/test.php and you should see a lot of PHP configuration information.
Congratulations! You now have Apache, MySQL and PHP installed in your computer. Now you can install your favorite script right on your Windows workstation.
Sanjib Ahmad, Freelance Writer and Product Consultant for Business.Marc8.com - Business Best Sellers.
Should I Learn PHP and MySQL or Not?
I’ve had quite a few people ask me this question: “Should I learn PHP and MySQL?”.
The answer is never easy. It all depends on what you’re trying to accomplish. Are you looking to be involved in your own website? Is it being run on PHP and MySQL? What would you like to do with your website?
There’s a million questions you can ask yourself. I, personally, believe that anyone who wants to become more involved with their own website or run their own Web Design and Development Company should, at the minimum, learn the basics of PHP and MySQL.
Why should you? Well, let’s take a few examples…
Maybe you want to create a Members are on your site and you want to be able to offer downloads, use a more interactive Contact script, show information dynamically for your customers (i.e. Time and Date, Their personal information, the information they searched for last, Billing information, Download history, username and/or password, etc) to make it easier for them to browse through your site.
Maybe you want to be able to use PHP for your Search Engine Optimization needs? Needs such as 404 Redirects, 301 redirects, etc.
Maybe you need to create a built in search system for your e-commerce site? Maybe you’re looking to simply redirect people to an Affiliate program?
See the many, MANY ways you can use PHP? It’s a very valuable tool to know and understand. Imagine yourself having the knowledge to take a product like Wordpress and fully customize it to your own, personal, needs?
Think you can make money off of one of your Wordpress Plugin Ideas by knowing PHP? Think you can build a better Content Management System than what’s currently available? What about a new Social Networking Script - OR Community?
Companies like PHPFox have made a killing with their php knowledge. Likewise for companies such as Mambo, vBulletin and various other scripts that you can purchase.
So the next question would be HOW you can learn PHP?
There’s a million ways to learn but I try to explain to everybody that watching Videos and DOING the coding practices they ask you to do is how you’ll learn quickly.
Sure… lot’s of people have learned PHP by reading books. But I find the problem with books is a lack of support. You’re always going to learn more by seeing and doing rather than reading and doing. Luckily there are solutions to this problem.
You can also visit popular forums such as Sitepoint, Digitalpoint and other Webmaster related forums or communities that allow other members to help with questions and answers.
No matter what you choose to do - just remember that DOING is how you learn. Practice without reading or watching. Just take the little knowledge you have at the time and make it grow with practice.
I hope you figure out what you need to accomplish and good luck!
Want to learn PHP at rapid speed? Check out these awesome Video Tutorials I’ve found!
If you’re looking to become a PHP programmer - you need to see these videos!
PHP, MySQL and ASP NET in Web Hosting
Choosing between LINUX web hosting and Windows hosting is a daunting task. You should know the language of your website, before determining which platform or which type of server you need. Linux hosting is best suitable for a simple HTML site or an ecommerce site or a site with any other programming-database feature created in php-MySql. If your site is in ASP, or ASP.NET, then you need to be on Windows servers.
Now what exactly are ASP.NET, PHP and MySQL web hosting? Let us delve further into these terms.
ASP.NET web hosting: Marketed by Microsoft, ASP.NET is a division of Microsoft’s .NET platform and a successor of Active Server Pages (ASP) technology. ASP.NET is a set of Web application development technologies that programmers can use to build dynamic applications, Web sites, and XML Web services.
ASP.NET web hosting cuts down the number of codes and makes building web application much easier. It gives more flexibility to the choice of language by supporting multiple .NET languages. In ASP.NET web hosting, as the source code and HTML are together, the pages are easy to write and maintain. Because of the built-in configuration information, registration of components is not necessary. ASP.NET application is faster and can face a large number of users with a consistency of performance.
PHP web hosting: PHP is a recursive initialism for PHP : Hypertext Preprocessor. PHP is a reflective programming language or a general-purpose server-side scripting language used for web development, i.e. creating dynamic and interactive websites. It can be planted into HTML code. PHP is frequently used together with APACHE on various operating systems mostly LINUX.
PHP acts more like a filter, running on a web server, taking inputs from a stream or file containing text and PHP instructions and displays a different stream of data. PHP is deployable in most operating systems, servers and platforms free of cost.
MySQL web hosting: MySQL is a multi-user, multithreaded computer language designed for management and retrieval of data, database object access control management and database schema creation and modification. It is a SQL database management system with 10 million installations. It is popular for web application, for open-source bug tracking tools and acts as the database component of WAMP, MAMP and LAMP. MySQL is closely related to PHP and Ruby on Rails. Most of the well known sites like Wordpress run on a combination of PHP and MySQL.
Now that you are more familiar with ASP.NET web hosting, PHP and MySQL web hosting, it may prove fruitful in your selection of server type.