Php

Run PHP and Mysql from the Command line in Windows

This tutorial will cover How to run php script from the command line and Run MySQL from the command line in windows, Sometimes we have big file and run through the command line instead of browser.

Therefore, we must use the command line to execute the PHP script. While PHPMyAdmin UI cannot import huge data files, we can import them using the command line from a SQL file to the database.

So, in this section, I’ll talk about how setup MySQL and PHP in Windows using the command line. PHP and MySQL need to be set up on Windows computers.

Run PHP script from command line in Windows

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

Option 1: Using Environment Variable

You can set your application path in the windows environment variable and access that application at root of the window command.

To access PHP from the command line you need to set the PHP path (‘c:\wamp\bin\php\php5.x.x1’) as an environment variable in windows.

When you set this variable you need to check with help of the below command it’s working fine.

php -v

If the above command returns the PHP version, everything is in order; otherwise, reopen the window and check the aforementioned command again.

Option 2: Without Environment Variable

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

a) To open start>run->cmd window.
b) go to you php path c:\wamp\bin\php\ and run the 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 errors like,

Related Post

‘file not found on location/not included’

that means you have included the file as a relative path, so you need to be changed relative to an 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 the below command from the 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

use test

d) Check db is updated.

mysql \r

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

How To Run a PHP script in Background Process

Sometimes, we need to set a PHP script as a background process in the system. Here, we learn about setting a PHP script in the LINUX system through the command line, it’s very easy to use. We need to run the below command and the script will run as a background process.

Command to execute PHP code in the background process

nohup php test.php &

the & puts your process in the background.

If you want to stop/kill this PHP script, then you need to run the below command:

kill processid

Conclusion:

This quick article explains how to set up PHP on the command line and how to execute PHP scripts from the command line. MySQL is also used via the window command line. MySQL is available for command-line use.

View Comments

Recent Posts

Configure and Retrieve S3 Information Using Laravel PHP-AWS-SDK

This tutorial helps integrate a PHP SDK with Laravel. We'll install aws-php-sdk into laravel application and access all aws services… Read More

3 months ago

What is the Purpose of php_eol in PHP?

in this quick PHP tutorial, We'll discuss php_eol with examples. PHP_EOL is a predefined constant in PHP and represents an… Read More

9 months ago

Laravel Table Relationship Methods With Example

This Laravel tutorial helps to understand table Relationships using Elequonte ORM. We'll explore laravel table Relationships usage and best practices… Read More

9 months ago

Exploring the Power of Laravel Eloquent Join?

We'll explore different join methods of Laravel eloquent with examples. The join helps to fetch the data from multiple database… Read More

9 months ago

Quick and Easy Installation of Laravel Valet

in this Laravel tutorial, We'll explore valet, which is a development environment for macOS minimalists. It's a lightweight Laravel development… Read More

10 months ago

What is Laravel Soft Delete and How Does it Work?

I'll go through how to use soft delete in Laravel 10 in this post. The soft deletes are a method… Read More

10 months ago

Categories