Your SlideShare is downloading. ×
  • Like
Mysql & Php
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Now you can save presentations on your phone or tablet

Available for both IPhone and Android

Text the download link to your phone

Standard text messaging rates apply

Mysql & Php

  • 2,960 views
Published

 

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
2,960
On SlideShare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
29
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. MYSQL & PHP Inbal Geffen
  • 2. What is MySQL? ● MySQL is a database server ● MySQL is ideal for both small and large applications ● MySQL supports standard SQL ● MySQL compiles on a number of platforms ● MySQL is free to download and useInbal Geffen
  • 3. What is MySQL?The data in MySQL is stored in database objectscalled tables.A table is a collection of related data entries and itconsists of columns and rows.Databases are useful when storing informationcategorically. A company may have a database withthe following tables: "Employees", "Products","Customers" and "Orders".Inbal Geffen
  • 4. Database TablesA database most often contains one or more tables.Each table is identified by a name (e.g. "Customers"or "Orders"). Tables contain records (rows) withdata. LastName FirstName Age City Jill Jack 30 NY Cruise Tom 23 NY Bradshaw Kari 30 NYBelow is an example of a table called "Persons":The table above contains three records (one foreach person) and four columns (LastName,FirstName, Age, and City).Inbal Geffen
  • 5. QueriesA query is a question or a request.With MySQL, we can query a database for specificinformation and have a recordset returned.Look at the following query:SELECT LastName FROM PersonsThe query above selects all the data in the"LastName" column from the "Persons" table, andwill return a recordset like this: LastName Jill Cruise BradshawInbal Geffen
  • 6. PHP+MySQL - Connect to a DatabaseBefore you can access data in a database, you mustcreate a connection to the database.In PHP, this is done with the mysql_connect()function.Syntaxmysql_connect(servername,username,password);<?php$con = mysql_connect("localhost","peter","abc123");if (!$con) { die(Could not connect: . mysql_error()); }// some code?>Inbal Geffen
  • 7. Closing a connectionThe connection will be closed automatically whenthe script ends.To close the connection before, use themysql_close() function:<?php$con = mysql_connect("localhost","peter","abc123");if (!$con) { die(Could not connect: . mysql_error()); }// some codemysql_close($con);?>Inbal Geffen
  • 8. Create a database<?php$con = mysql_connect("localhost","peter","abc123");if (!$con) { die(Could not connect: . mysql_error()); }if (mysql_query("CREATE DATABASE my_db",$con)) { echo "Database created"; }else { echo "Error creating database: " . mysql_error(); }mysql_close($con);?>Inbal Geffen
  • 9. Create a table<?php$con = mysql_connect("localhost","peter","abc123");if (!$con) { die(Could not connect: . mysql_error()); }// Create databaseif (mysql_query("CREATE DATABASE my_db",$con)) { echo "Database created";}else { echo "Error creating database: " . mysql_error();}// Create tablemysql_select_db("my_db", $con); //A database must be selected before a table can be created$sql = "CREATE TABLE Persons(FirstName varchar(15),LastName varchar(15),Age int)";// Execute querymysql_query($sql,$con);mysql_close($con);?>Inbal Geffen
  • 10. Insert Data Into a Database Table<?php$con = mysql_connect("localhost","peter","abc123");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 (Jack, Jill,33)");mysql_close($con);?>Inbal Geffen
  • 11. Insert Data From a Form Into a Database<html><body><form action="insert.php" method="post">Firstname: <input type="text" name="firstname">Lastname: <input type="text" name="lastname">Age: <input type="text" name="age"><input type="submit"></form></body></html>When a user clicks the submit button in the HTML form in theexample above, the form data is sent to "insert.php".The "insert.php" file connects to a database, and retrieves the valuesfrom the form with the PHP $_POST variables.Then, the mysql_query() function executes the INSERT INTOstatement, and a new record will be added to the "Persons" table.Inbal Geffen
  • 12. Insert Data From a Form Into a Database<?php$con = mysql_connect("localhost","peter","abc123");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);?>Inbal Geffen
  • 13. Select Data From a Database Table<?php$con = mysql_connect("localhost","peter","abc123");if (!$con) { die(Could not connect: . mysql_error()); }mysql_select_db("my_db", $con);$result = mysql_query("SELECT * FROM Persons");while($row = mysql_fetch_array($result)) { echo $row[FirstName] . " " . $row[LastName]; echo "<br />"; }mysql_close($con);?>Inbal Geffen
  • 14. Display the Result in an HTML Table<?php$con = mysql_connect("localhost","peter","abc123");if (!$con) { die(Could not connect: . mysql_error()); }mysql_select_db("my_db", $con);$result = mysql_query("SELECT * FROM Persons");echo "<table border=1><tr><th>Firstname</th><th>Lastname</th></tr>";while($row = mysql_fetch_array($result)) { echo "<tr>"; echo "<td>" . $row[FirstName] . "</td>"; echo "<td>" . $row[LastName] . "</td>"; echo "</tr>"; }echo "</table>";mysql_close($con);?>Inbal Geffen
  • 15. Display the Result in an HTML Table<?php$con = mysql_connect("localhost","peter","abc123");if (!$con) { die(Could not connect: . mysql_error()); }mysql_select_db("my_db", $con);$result = mysql_query("SELECT * FROM Persons");echo "<table border=1><tr><th>Firstname</th><th>Lastname</th></tr>";while($row = mysql_fetch_array($result)) { echo "<tr>"; echo "<td>" . $row[FirstName] . "</td>"; echo "<td>" . $row[LastName] . "</td>"; echo "</tr>"; }echo "</table>";mysql_close($con);?>Inbal Geffen
  • 16. Where<?php$con = mysql_connect("localhost","peter","abc123");if (!$con) { die(Could not connect: . mysql_error()); }mysql_select_db("my_db", $con);$result = mysql_query("SELECT * FROM PersonsWHERE FirstName=Jack");while($row = mysql_fetch_array($result)) { echo $row[FirstName] . " " . $row[LastName]; echo "<br>"; }?>Inbal Geffen
  • 17. Order By<?php$con = mysql_connect("localhost","peter","abc123");if (!$con) { die(Could not connect: . mysql_error()); }mysql_select_db("my_db", $con);$result = mysql_query("SELECT * FROM Persons ORDER BY age");while($row = mysql_fetch_array($result)) { echo $row[FirstName]; echo " " . $row[LastName]; echo " " . $row[Age]; echo "<br>"; }mysql_close($con);?>Inbal Geffen
  • 18. Update<?php$con = mysql_connect("localhost","peter","abc123");if (!$con) { die(Could not connect: . mysql_error()); }mysql_select_db("my_db", $con);mysql_query("UPDATE Persons SET Age=36WHERE FirstName=Jack AND LastName=Jill");mysql_close($con);?>Inbal Geffen
  • 19. Delete<?php$con = mysql_connect("localhost","peter","abc123");if (!$con) { die(Could not connect: . mysql_error()); }mysql_select_db("my_db", $con);mysql_query("DELETE FROM Persons WHERE LastName=Griffin");mysql_close($con);?>Inbal Geffen