24 Aug 2015

PHP MySQL Connectivity, Mysqli Connectivity,MySQL Create Database,MySQL Insert, MySQL Update, MySQL Delete

PHP Mysql connectivity

Use the mysql_connect( ) function to established connection to the MySQL server.
To access the database functionality we have to make a connection to database using Php.
mysql_connect() function is used to establish the connection to mysql server.
four arguments need to be passed to mysql_connect() function.
hostname : if you are working on local system , you can use localhost or you can also provide ip address or server name
username : if there is a existing user , you can provide username. default username is ‘root’.
password : by default password is blank or null.
dbname : it is a optional field . it is basically a name of the database that need to be connected.
ParameterDescription
host(Server name)Either a host name(server name) or an IP address
usernameThe MySQL user name
passwordThe password to log in with
dbnameOptional. The database to be used when performing queries

Note : There are more available parameters, but the ones listed above are the most important.
In the following example we store the connection in a variable ($con) for later use in the script
Here localhost is server name. root is MySQL default user name. default password is blank and database name is my_db. mysql_error( ) function provides mysql connectivity error message.

MySQL Close Connection

after work with the database is done we have to close the connection using mysql_close() function
in which the connection to the database is passed.

Mysqli PHP Connectivity

How to connect PHP through mysqli.
Create a registration form with 4 four fields name,email id, mobile number and address.
Fill all the fields click on save data submit button to save your data inside “users” table using mysqli prepared statements insert.
To display all the values from “users” table click on disp data button it will display the values using mysqli prepared statements select.

Create Database and Table

PHP Script(PHP mysqli connection )

HTML Form

mysqliconnectivity_phptpoint

PHP MySQL Create Database

Create Database

The CREATE DATABASE statement is used to create a MySQL database.
Create database is used to create a MySQL database.
We must add the CREATE DATABASE statement to the mysql_query() function to execute the command.
Ex i(Create database)
In the above example first we create the connection with the database then we write statement to create a database of name employee after that this statement is passed to mysql_query() along with the connection variable of the database.

Create a table

The CREATE TABLE statement is used to create a table
Create table is used to create a table. while creating a table we can define several constraints such as primary key, unique key,auto_increment etc.
We must add the CREATE TABLE statement to the mysql_query() function to execute the command.
Ex (Create Table)
In the above example first we create the connection with the database then we select a database of name employee
then we write the statement to create a table,and this statement is passed to mysql_query().

PHP MySQL Insert

PHP MySQL Insert Query

The INSERT INTO statement is used to add new records to a database table.
The insert statement is used to add new records to a database table.
each time a new record is to be added we use INSERT INTO statement for that purpose.
there are two ways of inserting records either explicitly providing the column name
with values respectively or simply by providing values of table but doesn’t specify the
column name.
Syntax
The second way doesn’t specify the column names where the data will be inserted, only their values
Syntax
Ex
In the previous chapter we created a table named “empInfo”, with four columns; “emp_id”, “name”, “emailid” and “mobile”. Use the same table to insert values inside empInfo table.

HTML Form

In the above example the values such as name , eid , mobile number are fetched from the
form and inserted into empInfo table using INSERT INTO query.

Insert data from HTML form directly in database

HTML Form

In the above example values such as name , eid , mobile number are fetched from the
form and inserted into empInfo table without providing the column names

PHP MySQL Update

PHP MySQL Update Query

The update keyword is basically used to modify or edit the existing records in the database table.
it usually need a where clause to find out in which record change is to be done.
It is must to specify the where clause otherwise all records of that table got modify.
Syntax
Note : Notice the WHERE clause in the UPDATE syntax is must otherwise all records will be updated!
Ex
Previous records in empInfo table are:
Emp_idNameEmailMobile
1deveshdevesh@gmail.com9910099100
2deepakdeepak@gmail.com9210053520
3raviravi@gmail.com9810098100

This example updates some data in the “empInfo” table
In the above example ,
empInfo table has 3 records of devesh,deepak, and ravi.
we need updation in this table as
devesh change to dev and his mobile number also need to be changed.
here first we create connection with the database,then database is selected using mysql_select_db(),
then update query is passed to the mysql_query( ) and the database table is updated.

PHP MySQL Delete

PHP MySQL Delete

DELETE keyword is used basically to delete 1 or more than one records from the database.
even if we delete each and every row of a table then also the schema of the table remain un deleted.
it,s necessary to use where clause in delete query, otherwise all the records will be deleted.
Syntax
Note : Notice the WHERE clause in the DELETE syntax is must otherwise all records will be deleted!
Eg 1
Previous records in empInfo table are:
Emp_idNameEmailMobile
1deveshdevesh@gmail.com9910099100
2deepakdeepak@gmail.com9210053520
3raviravi@gmail.com9810098100

This example deleted one row from “empInfo” table

After deletion , the “empInfo” table will look like this:

Emp_idNameEmailMobile
2deepakdeepak@gmail.com9210053520
3raviravi@gmail.com9810098100
In the above example ,
empInfo table has 3 records of devesh,deepak, and ravi.
we need to delete a record from this table.
The record of devesh here is to be deleted.
here first we create connection with the database,then database is selected using mysql_select_db(),
then delete query is passed to the mysql_query and the table row is deleted.

No comments:

Post a Comment