How To Run PHP and Mysql from Command line in Windows

In this quick tutorial i will let you know how to run php and mysql through command line in windows, Sometime we have big file and run through command line instead of browser, So we need run php script with help of command line.We can also import big data file from sql file to database through command line whereas phpmyadmin UI do not able to import.

Run PHP and MySQL from command line in Windows

There are two ways to run PHP through command line.
1- Environment variable: For this you need to set PHP path (‘c:\wamp\bin\php\php5.x.x1’) as environment variable.
When you set this variable you need to check with help of below command its working fine.

Select Code
1
php -v

If above command return php version then its work fine otherwise restart window and again checked above command.

2- Without Environment Variable: Sometimes you don’t want to set environment variable you just need run your script. You need to follow steps:
a) To open start>run->cmd window.
b) go to you php path c:\wamp\bin\php\ and rub below command.

Select Code
1
php -v

It will give you php version number if you are on right path.

How to run PHP script:

you need to run below command to run php script file.

Select Code
1
php  script.php

Sometimes you will get error

‘file not found on location/not included’

, that means you have included file as relative path, so you need changed relative to absolute path.

Select Code
1
include_once  __DIR__ . '/../config.php';

How to run MYsql query through command line:

You need to go mysql path with help of below steps.

a) Open start>run->cmd window.
b) Go to your mysql path c:\wamp\bin\mysql\ and rub below command.

Select Code
1
mysql -u user -p

This command will ask root password , enter root password now you have login as root admin on mysql.

c)Now select db

Select Code
1
use test

c) Check db is updated.

Select Code
1
mysql \r

Now you have connected with 'Test' db you can run your sql query here.

I hope it help you.

  • RCMJanessa

    Thanks for finally talking about >How To run PHP and Mysql Through Command line in windows <Loved it!