Php db

254 views
216 views

Published on

Published in: Education, Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
254
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
4
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Php db

  1. 1. PHP and DatabasePHP and Database
  2. 2.  MysqlMysql – popular open-source database– popular open-source database management systemmanagement system  PHPPHP usually works withusually works with MysqlMysql for web-basedfor web-based database applicationsdatabase applications  LAMPLAMP applications—Web-based applicationsapplications—Web-based applications that usethat use LynuxLynux,, ApacheApache,, MysqlMysql, and, and php/pearl/pythonphp/pearl/python
  3. 3.  Connect to host server which has MysqlConnect to host server which has Mysql installedinstalled  Select a databaseSelect a database  Form an SQL statementForm an SQL statement  Execute the SQL statement and (optionally)Execute the SQL statement and (optionally) return a record setreturn a record set  Extract data from recordset using phpExtract data from recordset using php  Close connectionClose connection
  4. 4. <?php<?php $host = ‘localhost’;$host = ‘localhost’; $username = ‘peter’;$username = ‘peter’; $pswd = ‘!?+&*’;$pswd = ‘!?+&*’; $dbName = “myDB”;$dbName = “myDB”; $con = mysql_connect($host, $username,$con = mysql_connect($host, $username, $pswd);$pswd); if (!$con){if (!$con){ die('Could not connect: ‘die('Could not connect: ‘ . mysql_error());. mysql_error()); }} $db = mysql_select_db($dbName,$db = mysql_select_db($dbName, $con) or die(mysql_error());$con) or die(mysql_error()); ?>?>
  5. 5.  SQL  CREATE DATABASE database_name  PHP $con = mysql_connect("localhost","peter", "abc123"); $sql = “CREATE DATABASE myDB”; mysql_query(“$sql”, $con));
  6. 6.  SQL  CREATE TABLE table_name (column_name1 data_type, column_name2 data_type, column_name3 data_type, .... );
  7. 7.  PHP // Connect to Mysql $con = mysql_connect(. . .); // Create database mysql_query("CREATE DATABASE my_db",$con); // Select DB mysql_select_db("my_db", $con); // Create table $sql = "CREATE TABLE Persons( FirstName varchar(15), LastName varchar(15), Age int )”; // Execute SQL statement mysql_query($sql, $con); "; Contd:Contd:
  8. 8.  When DB already exists:When DB already exists:  PHPPHP $con = mysql_connect("localhost","peter",$con = mysql_connect("localhost","peter", "abc123");"abc123"); $db = mysql_select_db("my_db“,$db = mysql_select_db("my_db“, $con);$con);
  9. 9.  SQL SELECT colName1, colName2, colName3SELECT colName1, colName2, colName3 FROM Persons;FROM Persons;  PHP $con = mysql_connect(. . .);$con = mysql_connect(. . .); mysql_select_db("my_db“, $con);mysql_select_db("my_db“, $con); $sql = “SELECT FirstName, LastName$sql = “SELECT FirstName, LastName FROM Persons;”;FROM Persons;”; $result = mysql_query($sql);$result = mysql_query($sql);
  10. 10.  PHP $result = mysql_query($sql);$result = mysql_query($sql); while($row =while($row = mysql_fetch_array($result)){mysql_fetch_array($result)){ echo $row['FirstName'] . " " .echo $row['FirstName'] . " " . $row['LastName'];$row['LastName']; echo "<br />";echo "<br />"; }}
  11. 11.  SQL INSERT INTO table_nameINSERT INTO table_name VALUES (value1, value2, value3,...)VALUES (value1, value2, value3,...) oror INSERT INTO table_nameINSERT INTO table_name (column1, column2,(column1, column2, column3,...)column3,...) VALUES (value1, value2, value3,...)VALUES (value1, value2, value3,...)
  12. 12. <?php $con = mysql_connect(“…",“…",“…"); if (!$con) die('Could not connect: ' . mysql_error()); mysql_select_db("my_db", $con); mysql_query("INSERT INTO Persons (FirstName, LastName, Age) VALUES ('Peter','Griffin','35')"); mysql_query("INSERT INTO Persons (FirstName, LastName, Age) VALUES ('Glenn', ‘Brown', '33')"); mysql_close($con); ?> Contd:Contd:
  13. 13. <html><html> <body><body> <form action="insert.php" method="post"><form action="insert.php" method="post"> Firstname: <input type="text" name=“nameF" />Firstname: <input type="text" name=“nameF" /> Lastname: <input type="text" name=“nameL" />Lastname: <input type="text" name=“nameL" /> Age: <input type="text" name="age" />Age: <input type="text" name="age" /> <input type="submit" /><input type="submit" /> </form></form> </body></body> </html></html>
  14. 14. <?php $con = mysql_connect(“…",“…",“…"); if (!$con) die('Could not connect: ' . mysql_error()); mysql_select_db("my_db", $con); $sql="INSERT INTO Persons (FirstName, LastName, Age) VALUES ('$_POST[firstname]', '$_POST[lastname]', '$_POST[age]')"; if (!mysql_query($sql,$con)) die('Error: ' . mysql_error()); echo "1 record added"; mysql_close($con) ?> Contd:Contd:
  15. 15.  SQL UPDATE table_nameUPDATE table_name SET column1=value, column2=value2,...SET column1=value, column2=value2,... WHERE some_column = some_value;WHERE some_column = some_value;
  16. 16. <?php $con = mysql_connect(“…",“…",“…"); if (!$con) die('Could not connect: ' . mysql_error()); mysql_select_db("my_db", $con); $sql = “UPDATE Persons SET Age = ’36’ WHERE FirstName = ‘Peter’ AND LastName = “Griffin’”; mysql_query($sql, $con); mysql_close($con); ?> Contd:Contd:
  17. 17.  SQL Delete table_nameDelete table_name WHERE some_column = some_value;WHERE some_column = some_value;
  18. 18. <?php $con = mysql_connect(“…",“…",“…"); if (!$con) die('Could not connect: ' . mysql_error()); mysql_select_db("my_db", $con); $sql = “DELETE FROM Persons WHERE FirstName = ‘Peter’ AND LastName = “Griffin’”); mysql_query($sql, $con); mysql_close($con); ?> Contd:Contd:

×