Professional Documents
Culture Documents
Chapter 6 PHP and Databases
Chapter 6 PHP and Databases
C S 2 2 0 S P R I N G 2020
TBS 2019-2020
Reminder (1/2)
2
An interaction with a
database is possible on
the server side.
Reminder (2/2)
3
Interpreter
Introduction to PHP and databases (1/3)
4
MySQL is:
A DBMS that has many advantages such as: rapidity, easy use, robustness, free, etc.
Generally suitable and adapted to the majority of the small and medium sized
applications.
PHP and MySQL are generally combined in the same offer by hosting platforms.
Introduction to PHP and databases (2/3)
5
Prerequisites:
The SQL language (Structured Query Language) to query databases
through SQL requests
Database administration with PhpMyAdmin (available with XAMPP):
create, modify, add and delete databases and tables, etc.
Launch PhpMyAdmin :
Go to : http://localhost/phpmyadmin
Or,
via Xampp :
Start MySQL
and click on Admin
Introduction to PHP and databases (3/3)
6
MySQL
Mysqli
PDO (PHP Data Objects)
MySQL vs Mysqli
8
PDO vs Mysqli:
PDO works on 12 different database systems (JDBC,DBI,..), whereas
Mysqli only works with MySQL databases.
But, PDO doesn't allow to use all of the advanced features available
in Mysqli (e.g. support for Multiple Statements).
Both are object-oriented, but MySQLi also offers a procedural API.
Procedural API: functions are called to carry out tasks.
Object-oriented API: classes are instantiated and then methods
are called on the resulting objects.
PDO vs Mysqli (2/2)
10
Conclusion:
$user: the username authorized to access the database server, in our case it is
“root”
$pass: the associated password, in our case it is “” (empty) (to change your XAMPP
root MySQL password: http://veerasundar.com/blog/2009/01/how-to-change-the-
root-password-for-mysql-in-xampp/ )
Selecting a database:
$conn = mysqli_connect($host,$user,$pass,$dbname)
$dbname: the name of the database on which you want to operate (the
server may contain several databases.)
Once the database is chosen, all the SQL requests are sent to this
database without having to specify it.
Example: connecting and choosing a database
13
1) Function definition
<?php
function connectDB($db)
{
$conn = mysqli_connect("localhost","root","", $db);
if (!$conn)
{ die("Connection failed: " .mysqli_connect_error());
}
Return true;
}
2) Function call
$v = connectDB("test");
if($v == true)
{
echo "Connection to the db test is successful";
}
else echo "Could not connect to the server of the database";
?>
The PHP-MySQLi functions (3/4)
14
Interrogating a database:
mysqli_query($conn,$req)
$req: a character string containing the text of the request sent to the
database server. This string must not finish with inverted commas “;”.
<?php
$conn=mysqli_connect("localhost","root","","Library");
if ($conn==true)
{
$req = "SELECT * FROM Books WHERE author ='Balzac' ";
$res = mysqli_query($conn,$req);
if ($res==false)
echo "Cannot read the table";
else
…
}
?>
The PHP-MySQL functions (4/4)
16
mysqli_fetch_assoc ($res)
$res is the result returned by the function mysqli_query().
mysqli_fetch_assoc() returns a table containing one row of the result.
Each call to this function returns the next row until it returns false (meaning there
are no rows left.)
The named keys of the array are the names of the attributes in the original request.
The content of each array box is the value of the attribute on the database.
Example
17
database
created
Solution: Step 2
19
3) Create its columns: id (int), l_name (text), f_name (text), grade (float).
Solution: Step 3
21
SQL code
automatically
generated
Solution: Step 4
23
o SQL option -> write several SQL insert query (one for each row)
Solution: Step 4
24
o SQL option -> write one SQL insert query (for all the rows)
Solution: Step 4
25
o Import option -> importing data to your table from CSV file.
A Comma-Separated Values (CSV) file stores tabular data in clear
text.
Use Notepad++ or Excel to copy your data (slide 17) and save the
file as CSV file (data.csv).
Notepad++
Excel
Solution: Step 4
26
5) Display display on PhpMyAdmin all the fields of the students who have
grades above 10.
o SQL option -> write a SELECT query
Solution: Step 5
29
--> Output on a web page : write a Php code -> to write on Notepad ++
<?php
$conn=mysqli_connect("localhost","root","", "Results");
if (!$conn)
{
die("Connection failed: " . mysqli_connect_error());
}
$req = "SELECT * FROM student WHERE grade >= 10.00";
$res = mysqli_query($conn,$req);
if (mysqli_num_rows($res) > 0)
{
While($line = mysqli_fetch_assoc($res))
{
echo $line[‘id’]." ". $line[‘l_name’]." ". $line[‘f_name’]." ". $line[‘grade’];
echo "<br>";
}
}
else {echo "0 results";}
mysqli_close($conn);
?>
Export a database
31
Export option
PhpMyAdmin generates an SQL file
(.sql) containing SQL queries (DDL,
DML).