0% found this document useful (0 votes)
80 views20 pages

Chapter 19: PHP By: Eyad Alshareef

This document discusses connecting to and interacting with MySQL databases using PHP. It covers opening a database connection by calling mysqli_connect, selecting a database with mysqli_select_db, executing queries with mysqli_query, retrieving data from queries, and closing the connection with mysqli_close. Examples are provided for inserting, updating, deleting and selecting data from MySQL tables.

Uploaded by

Mustafa Radaideh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
80 views20 pages

Chapter 19: PHP By: Eyad Alshareef

This document discusses connecting to and interacting with MySQL databases using PHP. It covers opening a database connection by calling mysqli_connect, selecting a database with mysqli_select_db, executing queries with mysqli_query, retrieving data from queries, and closing the connection with mysqli_close. Examples are provided for inserting, updating, deleting and selecting data from MySQL tables.

Uploaded by

Mustafa Radaideh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

PHP

Chapter 19: PHP


By: Eyad Alshareef

Eyad Alshareef
Eyad Alshareef
19.9 Reading from a Database

 Function mysqli_connect connects to the MySQL database. It


takes three arguments—
 the server’s hostname
 a username
 a password
and returns a database handle—a representation of PHP’s
connection to the database, or false if the connection fails.

©1992-2012 by Pearson Education, Inc. All Rights Reserved.


1- Opening Database Connection:

$Conn = mysqli_connect(server, user ,password);

Parameter Description

server must - The host name running database server. If not


specified then default value is localhost
user must - The username accessing the database. If not
specified then default is the name of the user that owns
the server process. root
passwd Optional - The password of the user accessing the
database. If not specified then default is an empty
password.

Eyad Alshareef
2 - Selecting a Database

 Once you establish a connection with a database server then it is required to select a
particular database where your all the tables are associated.
 PHP provides function mysqli_select_db to select a database. It returns TRUE on success
or FALSE on failure.

bool mysql_select_db(connection , db_name );

Parameter Description
db_name Required - Database name to be selected
connection Required - if not specified then last opend
connection by mysql_connect will be used.

Eyad Alshareef
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname); // ----- 1+2
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());

mysqli_close($conn); // ------ 5
}
?>

Eyad Alshareef
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Create connection
$conn = mysqli_connect($servername, $username, $password); // ----- 1
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
$DataBase = mysqli_select_db ($conn , $db); // ------2

mysqli_close($conn); // ------ 5
}
?>
Eyad Alshareef
3 – Execute a Query

bool mysqli_query(connection , sql);

Parameter Description
sql Required - SQL query to execute
bool mysql_query( sql, connection );
connection Required - if not specified then last opend
connection by mysql_connect will be used.

Eyad Alshareef
Queries types
 Create a database
$sql = 'CREATE Database test_db';
 Creating Database Tables:
sql = 'CREATE TABLE employee( '.
'emp_id INT NOT NULL AUTO_INCREMENT, '.
'emp_name VARCHAR(20) NOT NULL, '.
'emp_address VARCHAR(20) NOT NULL, '.
'emp_salary INT NOT NULL, '.
'join_date timestamp(14) NOT NULL, '.
'primary key ( emp_id ))';

$retval = mysqli_query($conn , $sql);


Eyad Alshareef
 Insert Data into MySQL Database
$sql = "INSERT INTO employee ".
"(emp_name,emp_address, emp_salary, join_date) ".
"VALUES('$emp_name','$emp_address',$emp_salary, NOW())";

$retval = mysqli_query($conn , $sql);


 Deleting Data from MySQL Database
$sql = "DELETE employee ".
"WHERE emp_id = $emp_id" ;

$retval = mysqli_query($conn , $sql);

 Updating Data into MySQL Database


$sql = "UPDATE employee ".
"SET emp_salary = $emp_salary ".
"WHERE emp_id = $emp_id" ;

$retval = mysqli_query($conn , $sql);


Eyad Alshareef
4- Getting Data From MySQL Database

 <?php
$result = mysqli_query($conn , "SELECT id,email FROM people WHERE id = '42'");
if (!$result) {
    echo 'Could not run query: ' . mysqli_error($conn);
    exit;
}
$row = mysqli_fetch_row($result);
echo $row[0]; // 42
echo $row[1]; // the email value
?>

Eyad Alshareef
 $sql = "SELECT id, firstname, lastname FROM MyGuests";
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {    // output data of each row


    while($row = mysqli_fetch_assoc($result)) {
        echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " .
$row["lastname"]. "<br>";
    }
} else {
    echo "0 results";
}

Eyad Alshareef
$result = mysqli_query($Conn , $sql2);
while ($row = mysqli_fetch_row($result))
{
foreach ($row as $key => $value )
print $value."<br>";
}

Eyad Alshareef
5-Closing Database Connection:

bool mysqli_close ($link_identifier );

Eyad Alshareef
©1992-2012 by Pearson Education, Inc. All Rights Reserved.
©1992-2012 by Pearson Education, Inc. All Rights Reserved.
©1992-2012 by Pearson Education, Inc. All Rights Reserved.
©1992-2012 by Pearson Education, Inc. All Rights Reserved.
©1992-2012 by Pearson Education, Inc. All Rights Reserved.
©1992-2012 by Pearson Education, Inc. All Rights Reserved.

You might also like