SlideShare a Scribd company logo
MYSQL & PHP
  Inbal Geffen
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 use

Inbal Geffen
What is MySQL?

The data in MySQL is stored in database objects
called tables.

A table is a collection of related data entries and it
consists of columns and rows.

Databases are useful when storing information
categorically. A company may have a database with
the following tables: "Employees", "Products",
"Customers" and "Orders".


Inbal Geffen
Database Tables


A database most often contains one or more tables.
Each table is identified by a name (e.g. "Customers"
or "Orders"). Tables contain records (rows) with
data.
               LastName   FirstName   Age   City
               Jill       Jack        30    NY
               Cruise     Tom         23    NY
               Bradshaw   Kari        30    NY



Below is an example of a table called "Persons":
The table above contains three records (one for
each person) and four columns (LastName,
FirstName, Age, and City).
Inbal Geffen
Queries


A query is a question or a request.
With MySQL, we can query a database for specific
information and have a recordset returned.
Look at the following query:

SELECT LastName FROM Persons

The query above selects all the data in the
"LastName" column from the "Persons" table, and
will return a recordset like this:
                                LastName

                                Jill

                                Cruise

                                Bradshaw
Inbal Geffen
PHP+MySQL - Connect to a Database

Before you can access data in a database, you must
create a connection to the database.
In PHP, this is done with the mysql_connect()
function.
Syntax
mysql_connect(servername,username,password);

<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

// some code
?>


Inbal Geffen
Closing a connection

The connection will be closed automatically when
the script ends.
To close the connection before, use the
mysql_close() function:

<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

// some code

mysql_close($con);
?>




Inbal Geffen
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
Create a table

<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

// Create database
if (mysql_query("CREATE DATABASE my_db",$con))
  { echo "Database created";}
else
  { echo "Error creating database: " . mysql_error();}

// Create table
mysql_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 query
mysql_query($sql,$con);

mysql_close($con);
?>


Inbal Geffen
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
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 the
example above, the form data is sent to "insert.php".

The "insert.php" file connects to a database, and retrieves the values
from the form with the PHP $_POST variables.

Then, the mysql_query() function executes the INSERT INTO
statement, and a new record will be added to the "Persons" table.

Inbal Geffen
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
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
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
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
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 Persons
WHERE FirstName='Jack'");

while($row = mysql_fetch_array($result))
   {
   echo $row['FirstName'] . " " . $row['LastName'];
   echo "<br>";
   }
?>




Inbal Geffen
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
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=36
WHERE FirstName='Jack' AND LastName='Jill'");

mysql_close($con);
?>




Inbal Geffen
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

More Related Content

PDF
Jqeury ajax plugins
PDF
CakeFest 2013 keynote
PPTX
FYBSC IT Web Programming Unit V Advanced PHP and MySQL
PPT
Database presentation
PPTX
Database Connectivity in PHP
PDF
Introduction to php database connectivity
PPT
Synapse india reviews on php and sql
Jqeury ajax plugins
CakeFest 2013 keynote
FYBSC IT Web Programming Unit V Advanced PHP and MySQL
Database presentation
Database Connectivity in PHP
Introduction to php database connectivity
Synapse india reviews on php and sql

What's hot (20)

PDF
Dependency Injection
PDF
Stored Procedure
PDF
Internationalizing CakePHP Applications
PPTX
Анатолий Поляков - Drupal.ajax framework from a to z
PPT
PHP - Getting good with MySQL part II
PDF
PHP with MySQL
PPT
Quebec pdo
PDF
Using php with my sql
PDF
DB API Usage - How to write DBAL compliant code
PDF
international PHP2011_Bastian Feder_jQuery's Secrets
KEY
Introducing CakeEntity
KEY
Introducing CakeEntity
PDF
Sqlite perl
PDF
Using web2py's DAL in other projects or frameworks
PDF
Pagination in PHP
PDF
Add edit delete in Codeigniter in PHP
PDF
Country State City Dropdown in PHP
PDF
Tips of CakePHP and MongoDB - Cakefest2011 ichikaway
KEY
Sprout core and performance
PDF
Web2py
Dependency Injection
Stored Procedure
Internationalizing CakePHP Applications
Анатолий Поляков - Drupal.ajax framework from a to z
PHP - Getting good with MySQL part II
PHP with MySQL
Quebec pdo
Using php with my sql
DB API Usage - How to write DBAL compliant code
international PHP2011_Bastian Feder_jQuery's Secrets
Introducing CakeEntity
Introducing CakeEntity
Sqlite perl
Using web2py's DAL in other projects or frameworks
Pagination in PHP
Add edit delete in Codeigniter in PHP
Country State City Dropdown in PHP
Tips of CakePHP and MongoDB - Cakefest2011 ichikaway
Sprout core and performance
Web2py
Ad

Similar to Mysql & Php (20)

PDF
4.3 MySQL + PHP
PPTX
Php mysq
PPTX
chapter_Seven Database manipulation using php.pptx
PPTX
Connecting_to_Database(MySQL)_in_PHP.pptx
PPSX
DIWE - Working with MySQL Databases
PPTX
Basics of Working with PHP and MySQL.pptx
PPTX
UNIT V (5).pptx
PPTX
Database Connectivity MYSQL by Dr.C.R.Dhivyaa Kongu Engineering College
PDF
The History of PHPersistence
PPTX
Practical MySQL.pptx
KEY
Php 101: PDO
PPTX
7. PHP and gaghhgashgfsgajhfkhshfasMySQL.pptx
PDF
PHP and Rich Internet Applications
PDF
Everything About PowerShell
PPTX
PHP DATABASE MANAGEMENT.pptx
PPT
Php Mysql
ODP
Php 102: Out with the Bad, In with the Good
PDF
All Things Open 2016 -- Database Programming for Newbies
PDF
Pemrograman Web 8 - MySQL
PPT
Lecture6 display data by okello erick
4.3 MySQL + PHP
Php mysq
chapter_Seven Database manipulation using php.pptx
Connecting_to_Database(MySQL)_in_PHP.pptx
DIWE - Working with MySQL Databases
Basics of Working with PHP and MySQL.pptx
UNIT V (5).pptx
Database Connectivity MYSQL by Dr.C.R.Dhivyaa Kongu Engineering College
The History of PHPersistence
Practical MySQL.pptx
Php 101: PDO
7. PHP and gaghhgashgfsgajhfkhshfasMySQL.pptx
PHP and Rich Internet Applications
Everything About PowerShell
PHP DATABASE MANAGEMENT.pptx
Php Mysql
Php 102: Out with the Bad, In with the Good
All Things Open 2016 -- Database Programming for Newbies
Pemrograman Web 8 - MySQL
Lecture6 display data by okello erick
Ad

More from Inbal Geffen (8)

PDF
Web Storage & Web Workers
PDF
PDF
Jqeury ajax plugins
PDF
Jquery mobile2
PDF
Jquery2
PDF
J querypractice
PDF
J queryui
PDF
jQuery mobile UX
Web Storage & Web Workers
Jqeury ajax plugins
Jquery mobile2
Jquery2
J querypractice
J queryui
jQuery mobile UX

Mysql & Php

  • 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 use Inbal Geffen
  • 3. What is MySQL? The data in MySQL is stored in database objects called tables. A table is a collection of related data entries and it consists of columns and rows. Databases are useful when storing information categorically. A company may have a database with the following tables: "Employees", "Products", "Customers" and "Orders". Inbal Geffen
  • 4. Database Tables A database most often contains one or more tables. Each table is identified by a name (e.g. "Customers" or "Orders"). Tables contain records (rows) with data. LastName FirstName Age City Jill Jack 30 NY Cruise Tom 23 NY Bradshaw Kari 30 NY Below is an example of a table called "Persons": The table above contains three records (one for each person) and four columns (LastName, FirstName, Age, and City). Inbal Geffen
  • 5. Queries A query is a question or a request. With MySQL, we can query a database for specific information and have a recordset returned. Look at the following query: SELECT LastName FROM Persons The query above selects all the data in the "LastName" column from the "Persons" table, and will return a recordset like this: LastName Jill Cruise Bradshaw Inbal Geffen
  • 6. PHP+MySQL - Connect to a Database Before you can access data in a database, you must create a connection to the database. In PHP, this is done with the mysql_connect() function. Syntax mysql_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 connection The connection will be closed automatically when the script ends. To close the connection before, use the mysql_close() function: <?php $con = mysql_connect("localhost","peter","abc123"); if (!$con) { die('Could not connect: ' . mysql_error()); } // some code mysql_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 database if (mysql_query("CREATE DATABASE my_db",$con)) { echo "Database created";} else { echo "Error creating database: " . mysql_error();} // Create table mysql_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 query mysql_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 the example above, the form data is sent to "insert.php". The "insert.php" file connects to a database, and retrieves the values from the form with the PHP $_POST variables. Then, the mysql_query() function executes the INSERT INTO statement, 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 Persons WHERE 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=36 WHERE 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