How To Run PHP and Mysql from Command line in Windows

This tutorial will cover run php script from command line and Run MySQL from command line in windows, Sometimes 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.

So here i Will describe about configure MySQL and php from command line in windows.You must have MySQL and php install on your windows machines.

Run PHP script from command line in Windows

We can run PHP code from command line using following ways.You can use any of them and run PHP script from command line.

Option 1: Using Environment Variable

You can set your application path in windows environment variable and access that application on root of window command.
To access php from command line you need to set PHP path (‘c:\wamp\bin\php\php5.x.x1‘) as a environment variable in windows.
When you set this variable you need to check with help of below command its working fine.

php -v

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

Option 1: Without Environment Variable

You can also run php script from command line without setting environment variable in windows, Sometimes you don’t want to set environment variable you just need run your script from window command window. You need to follow following steps:

a) To open start>run->cmd window.
b) go to you php path c:\wamp\bin\php\ and run below command.

		php -v

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

How to run PHP script:

When you have configured php as command line in windows, You are ready to run php script using below command.

  php  script.php

Sometimes you will get error like,

‘file not found on location/not included’

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

 include_once  __DIR__ . '/../config.php';

How to run MySQL From command line:

We can run MySQL from command line using following way.You can use any of them and run MySQL from command line.You need to go mysql path (where you have installed mysql) with help of below steps.

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

   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
[code type=sql]
use test

d) Check db is updated.
[code type=sql]
mysql \r

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


This quick tutorial help to configure php from command line and run php script from command line.We have also use MySQL from window command line.We can run mysql from command line.