Date post: | 24-Jan-2015 |
Category: |
Education |
Upload: | subhasis-nayak |
View: | 873 times |
Download: | 2 times |
MyBlog = http://excellentprogramming.blogspot.com
PHP WITH MYSQL PART1CRUD- create, read, update, delete
04/1
0/2
3
1
Cop
y rig
ht o
f - Su
bh
asis
N
ayak
MyBlog = http://excellentprogramming.blogspot.com
PHP DATABASE INTERACTION Connecting to a database includes five steps.
Everytime you want to interact with database you must go through these five steps. These five steps as below Create a connection Select the database Perform databse query Use returned data / insert / updae / delete the
data. Close the connection
04/1
0/2
3
2
Cop
y rig
ht o
f - Su
bh
asis
N
ayak
MyBlog = http://excellentprogramming.blogspot.com
CREATE CONNECTION To connect your database you need your
credentials like user name, pass word and your server where your database is hosted.
Be carefull about those information those you are providing . It must be as exact as you get from your service provider or you cerated.
I will show you to work with database using “mysql” group of functions.
04/1
0/2
3
3
Cop
y rig
ht o
f - Su
bh
asis
N
ayak
MyBlog = http://excellentprogramming.blogspot.com
CONTINUE .... To connect to database we use
mysql_connect(“servername”,“username”,“password”); Servername = It must be “localhost” or else your domain
name Username = you must have a user account to connect to
your database. Password = you must have password to authenticate your
self to connect to your database. When we are connecting to database we have to
check that if any error occurs and unable to connect to database we must catch that for our testing purpose. We will do that with a if condition test.
04/1
0/2
3
4
Cop
y rig
ht o
f - Su
bh
asis
N
ayak
MyBlog = http://excellentprogramming.blogspot.com
CONNECTION ERROR FINDING
04/1
0/2
3
5
Cop
y rig
ht o
f - Su
bh
asis
N
ayak
MyBlog = http://excellentprogramming.blogspot.com
$SELECT DATABASE($MYSQL_SELECT_DB(X,Y)) To select the databse we must need two things
Databse name and Connection string mysql_select_db(“$database",$connect);
$databse= you have to write the name of your database you wanto connect
$connect = here we are passing the connection string like Servername Username Password
Here we must do a error testing. As we did for connection
04/1
0/2
3
6
Cop
y rig
ht o
f - Su
bh
asis
N
ayak
MyBlog = http://excellentprogramming.blogspot.com
DATABASE SELECTING ERROR FINDING
04/1
0/2
3
7
Cop
y rig
ht o
f - Su
bh
asis
N
ayak
MyBlog = http://excellentprogramming.blogspot.com
PERFORM DATABASE QUERY To perform a query operation we have to pass
a query string to the function “mysql_query(“$query”, $connect)”. $query may be a dtabse query like
“SELECT * FROM [table name] [condition]” “SELECT [column name] FROM [table name] [condition]”.
$connect = connection string which holds your Servername Username password
04/1
0/2
3
8
Cop
y rig
ht o
f - Su
bh
asis
N
ayak
MyBlog = http://excellentprogramming.blogspot.com
QUERY ERROR FINDING As we did a test for connecting to database
selecting a database we will do a test for query action. To do that we will uses if() a follows. if(!$result){die("Sorry, Unable to fetch
record".mysql_error());} Here it checks the function is really fecthing the
record or some errors there in our query function.
04/1
0/2
3
9
Cop
y rig
ht o
f - Su
bh
asis
N
ayak
MyBlog = http://excellentprogramming.blogspot.com
USE RETURNED DATA In this level you can insert, delete or update the
data. I will show you the fetched data here. You must
follow the mysql referece or some mysqlbooks for more. while($row=mysql_fetch_array($result)){
$pid=$row["p_id"];$linkid=$row["linklabel"];$content=$row["pagebody"]; echo $pid . “ ” . $linkid. “ ” . $content ; }Here “pid”, ”linklabe”, ”pagebody” are the coloumn names of the table.
04/1
0/2
3
10
Cop
y rig
ht o
f - Su
bh
asis
N
ayak
MyBlog = http://excellentprogramming.blogspot.com
CLOSE THE CONNECTION When our task will complete we will close the
database conncetion
04/1
0/2
3
11
Cop
y rig
ht o
f - Su
bh
asis
N
ayak
MyBlog = http://excellentprogramming.blogspot.com
04/1
0/2
3C
op
y rig
ht o
f - Su
bh
asis
N
ayak
12