Php With Mysql Part1

736 views
657 views

Published on

five step procedure to work with Mysql database

Published in: Education
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
736
On SlideShare
0
From Embeds
0
Number of Embeds
8
Actions
Shares
0
Downloads
59
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Php With Mysql Part1

  1. 1. PHP WITH MYSQL PART1 CRUD- create, read, update, delete 04/07/10 Copy right of - Subhasis Nayak
  2. 2. PHP DATABASE INTERACTION <ul><li>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 </li></ul><ul><ul><li>Create a connection </li></ul></ul><ul><ul><li>Select the database </li></ul></ul><ul><ul><li>Perform databse query </li></ul></ul><ul><ul><li>Use returned data / insert / updae / delete the data. </li></ul></ul><ul><ul><li>Close the connection </li></ul></ul>04/07/10 Copy right of - Subhasis Nayak
  3. 3. CREATE CONNECTION <ul><li>To connect your database you need your credentials like user name, pass word and your server where your database is hosted. </li></ul><ul><li>Be carefull about those information those you are providing . It must be as exact as you get from your service provider or you cerated. </li></ul><ul><li>I will show you to work with database using “mysql” group of functions. </li></ul>04/07/10 Copy right of - Subhasis Nayak
  4. 4. CONTINUE .... <ul><li>To connect to database we use </li></ul><ul><ul><li>mysql_connect( “servername”,“username”,“password” ); </li></ul></ul><ul><ul><ul><li>Servername = It must be “localhost” or else your domain name </li></ul></ul></ul><ul><ul><ul><li>Username = you must have a user account to connect to your database. </li></ul></ul></ul><ul><ul><ul><li>Password = you must have password to authenticate your self to connect to your database. </li></ul></ul></ul><ul><li>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. </li></ul>04/07/10 Copy right of - Subhasis Nayak
  5. 5. CONNECTION ERROR FINDING 04/07/10 Copy right of - Subhasis Nayak
  6. 6. $SELECT DATABASE($MYSQL_SELECT_DB(X,Y)) <ul><li>To select the databse we must need two things </li></ul><ul><ul><li>Databse name and Connection string </li></ul></ul><ul><ul><li>mysql_select_db(“$database&quot;,$connect); </li></ul></ul><ul><ul><ul><li>$databse= you have to write the name of your database you wanto connect </li></ul></ul></ul><ul><ul><ul><li>$connect = here we are passing the connection string like </li></ul></ul></ul><ul><ul><ul><ul><li>Servername </li></ul></ul></ul></ul><ul><ul><ul><ul><li>Username </li></ul></ul></ul></ul><ul><ul><ul><ul><li>Password </li></ul></ul></ul></ul><ul><li>Here we must do a error testing. As we did for connection </li></ul>04/07/10 Copy right of - Subhasis Nayak
  7. 7. DATABASE SELECTING ERROR FINDING 04/07/10 Copy right of - Subhasis Nayak
  8. 8. PERFORM DATABASE QUERY <ul><li>To perform a query operation we have to pass a query string to the function “ mysql_query(“$query”, $connect) ”. </li></ul><ul><ul><li>$query may be a dtabse query like </li></ul></ul><ul><ul><ul><li>“ SELECT * FROM [table name] [condition]” </li></ul></ul></ul><ul><ul><ul><li>“ SELECT [column name] FROM [table name] [condition]”. </li></ul></ul></ul><ul><ul><li>$connect = connection string which holds your </li></ul></ul><ul><ul><ul><li>Servername </li></ul></ul></ul><ul><ul><ul><li>Username </li></ul></ul></ul><ul><ul><ul><li>password </li></ul></ul></ul>04/07/10 Copy right of - Subhasis Nayak
  9. 9. QUERY ERROR FINDING <ul><li>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. </li></ul><ul><ul><li>if(!$result){die(&quot;Sorry, Unable to fetch record&quot;.mysql_error());} </li></ul></ul><ul><ul><li>Here it checks the function is really fecthing the record or some errors there in our query function. </li></ul></ul>04/07/10 Copy right of - Subhasis Nayak
  10. 10. USE RETURNED DATA <ul><li>In this level you can insert, delete or update the data. </li></ul><ul><li>I will show you the fetched data here. You must follow the mysql referece or some mysqlbooks for more. </li></ul><ul><ul><li>while($row=mysql_fetch_array($result)){ </li></ul></ul><ul><ul><li>$pid=$row[&quot;p_id&quot;]; </li></ul></ul><ul><ul><li>$linkid=$row[&quot;linklabel&quot;]; </li></ul></ul><ul><ul><li>$content=$row[&quot;pagebody&quot;]; </li></ul></ul><ul><ul><li>echo $pid . “ ” . $linkid. “ ” . $content ; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>Here “pid”, ”linklabe”, ”pagebody” are the coloumn names of the table. </li></ul></ul>04/07/10 Copy right of - Subhasis Nayak
  11. 11. CLOSE THE CONNECTION <ul><li>When our task will complete we will close the database conncetion </li></ul>04/07/10 Copy right of - Subhasis Nayak
  12. 12. 04/07/10 Copy right of - Subhasis Nayak

×