1 / 18

PHP and Database

PHP and Database. PHP and Database. Mysql – popular open-source database management system PHP usually works with Mysql for web-based database applications LAMP applications—Web-based applications that use Lynux , Apache , Mysql , and php/pearl/python. Basic Steps to Process DB.

rosie
Download Presentation

PHP and Database

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. PHP and Database

  2. PHP and Database • Mysql– popular open-source database management system • PHPusually works with Mysqlfor web-based database applications • LAMP applications—Web-based applications that use Lynux, Apache, Mysql, and php/pearl/python

  3. Basic Steps to Process DB • Connect to host server which has Mysql installed • Select a database • Form an SQL statement • Execute the SQL statement and (optionally) return a record set • Extract data from recordset using php • Close connection

  4. Connect to Mysql <?php$host = ‘localhost’;$username = ‘peter’;$pswd = ‘!?+&*’;$dbName = “myDB”;$con = mysql_connect($host, $username, $pswd);if (!$con){  die('Could not connect: ‘ . mysql_error());}$db = @mysql_select_db($dbName, $con) or die(mysql_error());?>

  5. Create a Database • SQL • CREATE DATABASE database_name • PHP$con = mysql_connect("localhost","peter", "abc123");$sql = “CREATE DATABASE myDB”;mysql_query(“$sql”, $con));

  6. Create a Table • SQL • CREATE TABLE table_name(column_name1 data_type,column_name2 data_type,column_name3 data_type, ....)

  7. Create a Table • PHP • PHP// Connect to Mysql$con = mysql_connect(. . .);// Create databasemysql_query("CREATE DATABASE my_db",$con);// Select DBmysql_select_db("my_db", $con);// Create table$sql = "CREATE TABLE Persons( FirstName varchar(15), LastName varchar(15), Age int )”;// Execute SQL statementmysql_query($sql, $con); ";

  8. Select a Database • When DB already exists: • PHP$con = mysql_connect("localhost","peter", "abc123");$db = mysql_select_db("my_db“, $con);

  9. Executing a SELECT Query • SQLSELECT colName1, colName2, colName3FROM Persons; • PHP$con = mysql_connect(. . .);mysql_select_db("my_db“, $con);$sql = “SELECT FirstName, LastName FROM Persons;”; $result = mysql_query($sql);

  10. Printing Results of SQL Statement • PHP$result = mysql_query($sql);while($row = mysql_fetch_array($result)){  echo $row['FirstName'] . " " . $row['LastName'];  echo "<br />";}

  11. Inserting Record into Table • SQLINSERT INTO table_nameVALUES (value1, value2, value3,...)orINSERT INTO table_name (column1, column2, column3,...)VALUES (value1, value2, value3,...)

  12. Inserting Record into Table <?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);?>

  13. Inserting Record into Table From HTML Form <html><body><form action="insert.php" method="post">Firstname: <input type="text" name=“nameF" />Lastname: <input type="text" name=“nameL" />Age: <input type="text" name="age" /><input type="submit" /></form></body></html>

  14. Inserting Record into Table From HTML Form <?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)?>

  15. Update Record • SQLUPDATE table_nameSET column1=value, column2=value2,...WHERE some_column = some_value;

  16. Update Record <?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);?>

  17. Delete Record • SQLDelete table_nameWHERE some_column = some_value;

  18. Delete Record <?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);?>

More Related