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.
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.
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.
Parameter | Description |
---|---|
host(Server name) | Either a host name(server name) or an IP address |
username | The MySQL user name |
password | The password to log in with |
dbname | Optional. 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
1
2
3
4
5
6
7
8
9
10
|
<?php
// Create connection
$con=mysql_connect("localhost","root","","my_db") or die(mysql_error());
//code to be executed...
// Close connection
mysql_close($con);
?>
|
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.
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 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
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)
Ex (Create Table)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
<?php
$con=mysql_connect("localhost","root","") or die(mysql_error());
// select database
mysql_select_db("Employee",$con);
// Create table
$sql="CREATE TABLE empInfo
(
emp_id int auto_increment primary key,
name char(50) not null,
email varchar(50) not null,
mobile bigint not null
)";
if (mysql_query($sql))
{
echo "Table empInfo created successfully";
}
else
{
echo "Error creating database: " .mysql_error();
}
?>
|
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().
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.
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.
form and inserted into empInfo table using INSERT INTO query.
Insert data from HTML form directly in database
HTML Form
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<form>
Enter your name<input type="text" name="name"/><hr/>
Enter your email<input type="text" name="eid"/><hr/>
Enter your mobile<input type="text" name="mob"/><hr/>
<input type="submit" value="INSERT"/><hr/>
</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
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 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
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_id | Name | Mobile | |
---|---|---|---|
1 | devesh | devesh@gmail.com | 9910099100 |
2 | deepak | deepak@gmail.com | 9210053520 |
3 | ravi | ravi@gmail.com | 9810098100 |
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.
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.
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.
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_id | Name | Mobile | |
---|---|---|---|
1 | devesh | devesh@gmail.com | 9910099100 |
2 | deepak | deepak@gmail.com | 9210053520 |
3 | ravi | ravi@gmail.com | 9810098100 |
This example deleted one row from “empInfo” table
After deletion , the “empInfo” table will look like this:
Emp_id | Name | Mobile | |
---|---|---|---|
2 | deepak | deepak@gmail.com | 9210053520 |
3 | ravi | ravi@gmail.com | 9810098100 |
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.
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.
then delete query is passed to the mysql_query and the table row is deleted.
No comments:
Post a Comment