AndroidCSS

Android material design Programming Blog

How to install and setup PHP and MySql in windows 10 and 7

By installing WAMP server on your windows you can run Apache, PHP, and MySql under a single package. Run sample PHP and MySql script.

In traditional web development environment to run PHP and MySQL on your machine, you need to install Apache, PHP and MySQL servers as a separate entity so that most of the time you’ll end up in seeing errors.

Rather than installing each server separately, how about installing a single package which contains all 3 servers? Yes, that is were WAMP comes into the picture.

WAMP is an open source web development package which in turn stands for Windows Apache MySql and PHP. WAMP is a package for the only windows environment, for any other platform like Linux, Apple OS you can use XAMP.

Advantages of using WAMP or XAMP

  • No need to install Apache, PHP and MySql servers separately.
  • Easy to manage and configure all functionality under a single menu.

Installing WAMP for Windows 10 and 7

The whole process is automatic and WAMP will take care of all major steps for you. Let’s see where I can find WAMP and what changes are necessary during installation.

  • Download the latest version of wamp server from wamp server website.
  • Run the installer, after accepting their agreement you will prompt to WAMP server location usually c:/wamp/. keep it as it is and click on next for all subsequent windows.wamp change drive location
  • You may ask for set your preferred browser and text-editor. leave default if you like else, change it.
    wamp setup choose browser
  • Finally, it will ask you to run wamp server or not. Make sure run wamp checkbox checked and click on the button below to run wamp server.

If everything is going well, you see a green wamp icon running on your system tray.wamp running green on tray

Error: Usually after the application launch, wamp icon color changes from red to green state(wait for some seconds).Even after waiting if you see a color of wamp icon other than green like red or orange, then there may be some errors. Find wamp server possible errors at the end of this article.

Setup MySql database

  • Click on wamp server icon on your system tray and go to phpMyAdmin.Wamp menu
  • you get a browser window asking for username and password. Type root as username and don’t enter anything in the password field(by default phpMyAdmin comes with root as MySQL username), Click on the login button to log in. You may set the password after login, under general settings → change password. Set root as password if you like.
    wamp setup PhpMyAdmin change password

You are done with setting up MySQL database, Now it’s time to run some simple PHP programs.

Sample program to test

1. Simple Hello world! program.

  • Create test.php file inside c:/wamp/www/ folder.
  • Copy the below code, paste it there and save the file.
    <?php
    
    	echo "Hello World!";
    
    ?>
    
  • Now, open your web browser and type http://localhost/test.php you see the output as below.
    PHP run sample program

2. Sample program to fetch data from database.

Here is the step by step procedure to create database and tables.

 

Adding database and tables into MySQL from phpMyAdmin.

Goto wamp icon → phpMyAdmin. From the left menu of phpMyAdmin page click on New button. Type the database name as “test” and click on create to create a new database.

Wamp PhpMyAdmin Create Database

After creating a database it will automatically prompt a window to create a table into it. Type table name as tbl_test, number of fields as 3 and click on create to create the table.

wamp phpmyadmin create table

Further, it will ask for datatype and size. Enter data shown in below image and click on go.

wamp PhpMyadmin choosing datatype for fields

Inserting values into the table.

With table selected in left menu click on insert button(tabbed menu on top) and enter the data as shown in below image. Click on go button to add your first row to the table.
wamp phpmyadmin insert data into table
You are done with adding data into the table. Now, will see how to fetch those data from PHP.

Fetching data from MySql using PHP

    • Create test_db.php file inside c:/wamp/www/ folder.
    • Copy the below code, paste it there and save the file.
<?php

$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "test";

try {
    	$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    	$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    }
catch(PDOException $e)
    {
    	die("OOPs something went wrong");
    }

$sql_query = 'SELECT * from tbl_test';
$stmt_query = $conn->prepare($sql_query);
$stmt_query->execute();

if($stmt_query->rowCount())
{
  while($row= $stmt_query->fetch(PDO::FETCH_ASSOC)){
 	 echo "Welcome, " . $row['first_name'] . " " . $row['middle_name'] . " " . $row['last_name'] . "\n";
  }

}
elseif(!$stmt_query->rowCount())
{
  echo "No data found.";		  
}
	
?>
  • Now, open your web browser and type http://localhost/test_db.php to run the program.
    php sample program to connect database and fetch data

The below portion of the article is only for one who finds an error while installing WAMP.

Possible errors while installing WAMP

1. Missing VCRUNTIME140.dll error

wamp dll missing error while installation

This error can be solved by downloading a newer version of visual c++ redistribution from Microsoft. Below is the link for visual c++ 2015 redistribution package, download and install it(both 32bit and 64bit versions available and make sure you are downloaded the same version you are running in).

Microsoft Visual C++ 2015 Redistribution

Uninstall WAMP and Reinstall.

2. Missing MSVCR110.dll error

Wamp missing MSVCR110.dll error

missing-msvcr110-dll-error

Solved by installing visual c++ 2012 redistribution from Microsoft. Below is the link for visual c++ 2012 redistribution package, download and install it(both 32bit and 64bit versions available and make sure you are downloaded the same version you are running in).

Microsoft Visual C++ 2012 Redistribution

Uninstall WAMP and Reinstall.

3. Port used by Microsoft IIS Server

This is the very common issue in Windows 10 and 7. Even after proper installation wamp icon does not turn to green. The reason is the port which is assigned to apache server is used by some other services on your PC(usually IIS Server).

Let’s check which service is using port 80. Go to wamp icon → Test the port 80.

If the port is used by IIS server here is the fix. Go to Services(Go to start menu of your windows
and start typing services to search for it), scroll to the bottom and find World Wide Web Publishing Services right click on it and disable.

Wamp disable world wide publishing windows 10

Now exit your wamp from the tray by Right click and exit then run it again.

4 Comments

Add yours

  1. Please do i need to install WAMPServer for Java Server Pages (JSP) web application? If No what are the softwares i need.

  2. Mario de Araujo Carvalho

    August 4, 2016 at 10:47 pm

    Muito bom o tutorial! 😀

Leave a Reply

Your email address will not be published.

*

About | Policy | Disclaimer

Creative Commons LicenceUp ↑