Showing posts with label MYSQL. Show all posts
Showing posts with label MYSQL. Show all posts

Monday, 22 February 2016

How to install PHP and MySQL in LINUX

PHP, MySQL and Apache are most important for web developers. Here i am going to show you how to install all of these in your LINUX machine. So let's start and follow my steps carefully.

Step 1 : First of all we need to install apache or any other web server. here we will install apache. Apache is a free open source software which runs over 50% of the world’s web servers. For installing apache on your machine open the terminal in your LINUX machine and type following commands one by one.

sudo apt-get update
sudo apt-get install apache2

Now open your browser and type "localhost" or "". It will show you the following page.

Step 2 : Now install MySQL on your machine. To install MySQL, open terminal and type in these commands.

sudo apt-get install mysql-server libapache2-mod-auth-mysql php5-mysql

In the middle of your installation it will ask you set the root password, If you missed then set the password later within the MySQL shell.
Once you have installed MySQL, we should activate it with this command: 

sudo mysql_install_db

Now type the following command:

sudo /usr/bin/mysql_secure_installation

After this a prompt will ask you for entering the current root password. Type the password. Now prompt will ask you to change the password, So type N.
After that type 'Y' in all upcoming prompts. At last MySQL will reload and implement the new changes. 
Now MySQL installed successfully. For checking it open the terminal and type the following command :

sudo mysql root your_password

Step 3 : Now we are going to install PHP. To install PHP, open terminal and type in this command.

sudo apt-get install php5 libapache2-mod-php5 php5-mcrypt

Type 'Y' in two prompts after it. It will install PHP automatically.
Now add php to the directory index by the following command:

sudo nano /etc/apache2/mods-enabled/dir.conf

Now add the index.php in beginning of the index files. It should be see like this :

<IfModule mod_dir.c>

          DirectoryIndex index.php index.html index.cgi index.php index.xhtml index.htm


Now type the command for adding the libraries and modules:

apt-cache search php5-

The terminal will show you the all modules. Select one of them and type the command:

sudo apt-get install name of the module

Now setup is almost complete. create a new file named "phpinfo.php" in '/var/www' folder and type the following lines.


Save it and open the browser and type "localhost/phpinfo.php"  the following address in address bar. It will show you the following screen.

Good luck guys. 

Thursday, 9 April 2015

How to use PHP to access a Database

Databases are backbone of today's websites. PHP has support for over 20 databases. MySQL, PostgreSQL and Oracle are widely used databases. Databases are used for storing users registration data, credit card numbers, purchase history, product reviews and many other things.
PHP and phpmyadmin both are used to access databases. Relational database management system is a server which is used to manage data. Data is structured into tables where each table have some no. of columns, each of which has a name and a type. PHP communicates with relational databases such as MySQL and Oracle using structured query language (SQL). The syntax for SQL is divided into two parts. The first, Data Manipulation Language, or DML, is used to retrieve and modify data in an existing database. DML is remarkably compact, consisting of only four verbs: select, insert, update, and delete. The set of SQL commands, used to create and modify the database structures that hold the data, is known as Data Definition Language, or DDL. The syntax for DDL is not as standardized as that for DML, but as PHP just sends any SQL commands you give it to the database, you can use any SQL commands your database supports.

Assuming you have a table called movies, this SQL statement would insert a new row:
INSERT INTO movies VALUES(0, 'fast and furious 7', 2015, 2)
This SQL statement inserts a new row but lists the columns for which there are values:
INSERT INTO movies (title, year, actor) VALUES ('hercules', 2015, 2)
To delete all movies from 2015, we could use this SQL statement:
DELETE FROM movies WHERE year=2015
To change the year for hercules to 2014, use this SQL statement:
UPDATE movies SET year=2014 WHERE title='hercules'
To fetch only the movies made in the 2010s, use:
SELECT * FROM movies WHERE year >= 2010 AND year < 2015 .

 Example :  

This example will show you how to insert form data into database using PHP.

1) First create a html form.

2) Now create a connect.php file.

How to reset root password of MYSQL on windows

Use the following procedure to reset the password for all MySQL root accounts:

1) Log on to your system as Administrator.

2) Stop the MySQL server if it is running. For a server that is running as a Windows service, go to the Services manager: From the Start menu, select Control Panel, then Administrative Tools, then Services. Find the MySQL service in the list and stop it.

3) Create a text file containing the following statements. Replace the password with the password that you want to use.
UPDATE mysql.user SET Password=PASSWORD('MyNewPass') WHERE User='root';

The UPDATE statement resets the password for all root accounts, and the FLUSH statement tells the server to reload the grant tables into memory so that it notices the password change.

4) Save the file. For this example, the file will be named C:\mysql-init.txt.

5) Open a console window to get to the command prompt: From the Start menu, select Run, then enter cmd as the command to be run.

6) Start the MySQL server with the special --init-file option (notice that the backslash in the option value is doubled): 

C:\> C:\mysql\bin\mysqld --init-file=C:\\mysql-init.txt

NOTE : If you installed MySQL to a location other than C:\mysql, adjust the command accordingly.
  • If you installed MySQL using the MySQL Installation Wizard, you may need to specify a --defaults-file option:
  C:\> "C:\Program Files\MySQL\MySQL Server 5.6\bin\mysqld.exe"
       --defaults-file="C:\\Program Files\\MySQL\\MySQL Server 5.6\\my.ini"

  • The appropriate --defaults-file setting can be found using the Services Manager: From the Start menu, select Control Panel, then Administrative Tools, then Services. Find the MySQL service in the list, right-click it, and choose the Properties option. The Path to executable field contains the --defaults-file setting.
7) After the server has started successfully, delete C:\mysql-init.txt.

Congratulations, you should now be able to connect to the MySQL server as root using the new password. Stop the MySQL server, then restart it in normal mode again. If you run the server as a service, start it from the Windows Services window. If you start the server manually, use whatever command you normally use.

Configuring MYSQL Server

Configuration of Mysql server begins with defining following types of networking options.

Mysql server configuration panel
Server Installer Overview
 Server configuration Types : it defines amount of system resources that will be assign to your Mysql server instance.
  • Developer : It is basically your personal workstation.It configure Mysql to use least amount of system memory.  
  • Server : It consumes medium amount of system memory.
  • Dedicated : It is a machine that is dedicated to running Mysql server because no other major application are running on this server such as web servers.This consumes most amount of system memory.
Connectivity : this option define how you will connect to Mysql.
  •  TCP/IP : This is the most eligible option for server because this will connect you with port otherwise only localhost connections will allowed.
  • Named pipe : Enable and define the pipe name, similar to using --enable named-pipe option.
  • Shared memory : enable and then define the memory name, similar to using --shared-memory option.
Advanced Configuration : This option provides additional logging option configuration.
Accounts and Roles : This is the next option under installation.In this option You must provide root password for your server logging.
if Mysql server is already installed then you should provide current root password 
Windows Service :
  • Now you configure the Windows Service details. This includes the service name, whether the MySQL Server should be loaded at startup, and how the Windows Service for MySQL Server is executed. 
Advanced Option :
The next configuration step is available if the Advanced Configuration option was checked. This section includes options that are related to the MySQL log files. Now click apply server configuration and then click execute.This is all about server configuration.
For starting server enter the following command on cmd :
C:\> "C:\Program Files\MySQL\MySQL Server 5.6\bin\mysqld".