Chapter 8 Accessing MySQL Using PHP

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 15

Chapter 8

Accessing MySQL Using PHP


Connection to MySQL Database
Three ways to connect to MySQL
• Connect to MySQL using legacy PHP functions. It is only a
procedural style.
• Connect to MySQL using MySQL improved. It is both
procedural and object oriented style.
• Connect to MySQL using PHP Data Objects (PDO). It is an
object oriented style.

2
Connection to MySQL
• To connect to database we create an object ($conn) of type
mysqli (i stands for improvements) function which in turn
takes four arguments: host name, user name, password, and
database name respectively.
For example,
$conn = new mysqli ("localhost", "root", "", "myDb");
if (!$conn->connect_error)
echo "Connected successfully";
else
die("Connection failed ");
3
The process of accessing the db
1. Connect to MySQL and select the database to use (mysqli( )
function).
$conn = new mysqli ("localhost", "root", "", "myDb");
2. Build a query string (sql statement).
$sql = "select * from tableName";
3. Perform and execute the query (query( ) method).
$result = $conn->query ($sql);
You can combine step 2 & 3:
$result = $conn->query ("select * from tableName");
4
The process of accessing the db (cont…)
4. Retrieve the results and output them to a web page
(fetch_assoc( ), fetch_row( ), fetch_array( ), function).
while ($row = $result->fetch_assoc())
echo ($row['id']. " ". $row['name']. "<br>");
5. Disconnect from MySQL (close( ) method).
$conn->close( )

5
Print column name
• To print table’s column name as header, use fetch_fields( )
function of query object.
• For example,
$c = $result->fetch_fields();
foreach($c as $val)
echo ("<th>". ucfirst($val->name));

6
Building and executing a query
• You can send a query to MySQL from PHP with mysqli
method using the query method of conn object. Once you
have an object returned in $result, you can use it to retrieve
the data you want, one item at a time, using the fetch_assoc,
fetch_row, or fetch_array method of the object.

• Note that table field names in the database are case sensitive.

7
fetch_array(), fetch_assoc(), fetch_row()
The difference is that:
• fetch_assoc( ): fetches a result row as an associative array.
• fetch_row( ): fetches a row as an enumerated array.
• fetch_array( ): etches a result row as an associative, a
numeric array, or both.

8
Insert Statement
$sql = "insert into students values (6, 'Yahya Ahmed Farah',
'Shangani')";
if ($conn->query($sql) == true)
echo ("Successfully registered");
Insert using form
$sql = "insert into students (id, fulname, address) values ('$no', '$n',
'$add')";
if ($conn->query($sql))
echo ("<br>Successfully registered");

9
Update Statement
$sql = "update students set fulname = 'Timira Adan Jimcale',
address = 'Waabari' where id = 'C120000'";
if ($conn->query($sql) == true)
echo ("<br>Successfully updated");

10
Delete Statement
$no = 'C119999';
$result = $conn->query ("select id from students where id =
'$no'");
if ($result->num_rows > 0) {
$sql = "delete from students where id = '$no'";
$conn->query($sql);
echo ("<br>Successfully deleted");
}

11
Delete Statement (cont…)
• At the end of the record:
echo("<td><a href='Delete.php?
Del=". $row['name']. "'>Delete</a>");

12
if (isset($_GET['Del'])) {
$name = $_GET['Del'];
require_once("Connection.php");
if (!$conn->connect_error) {
$result = $conn>query("select * from bit24 where fullname
= '$name'");
if ($result->num_rows > 0) {
$sql = "delete from students where fullname = '$name'";
if ($conn->query($sql) == true){
echo ("<br>has been deleted successfully.");
} } } }
13
Insert, update and delete,
• You can also do the same with insert, update and delete.

14
END
15

You might also like