1. Trang chủ
  2. » Mẫu Slide

Slide môn học PHP session 6 handling databases with PHP

24 161 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 24
Dung lượng 144 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Connecting to a Database Uses three arguments to connect PHP to MySQL such as server host name, user name, user password  Connectivity of PHP to MySQL are divided into three steps:

Trang 1

Handling Databases with PHP

Session 15

Trang 2

 A variable can store only one value at a time

 An array is a variable that can store a set of values of the same data type

 We can combine the element values of two or more arrays This process is called as merging arrays

 In a single-dimensional array, the element includes only one

level of key value pairs

 In the multidimensional array, each element is an array Each

element requires an array name and multiple set of indices

 Multidimensional arrays are arrays that store another array within

Trang 3

 Database APIs

 Connecting to a database

 Data access functions

 Performing SQL queries using PHP

 Building HTML tables using SQL queries

Trang 5

Connecting to a Database

 Uses three arguments to connect PHP to MySQL

such as server host name, user name, user

password

 Connectivity of PHP to MySQL are divided into

three steps:

 Connection with the MySQL server

 Working with the databases

 Closing the database connection

Trang 6

Connection with the MySQL

Trang 7

Connection with the MySQL

 host_name – Specifies the server or the host name

 user_name – Specifies the user name of MySQL

 password – Specifies the password for the MySQL user

 link_id - Specifies the return value of the server

connection

Trang 8

Example for connection with the MySQL server

 For example, to connect PHP to MySQL server

Trang 9

Working with the Databases

 Establish a connection with the MySQL server

 Needs to perform basic PHP functions while

working with the database such as

 mysql_list_dbs()

 mysql_select_db()

 mysql_list_tables()

 mysql_num_rows()

Trang 10

mysql_list_dbs() function

 Displays all the databases present in the server

 Syntax for this function is:

Trang 11

mysql_select_db() function

 Selects a database from the server

 Syntax for this function is:

Trang 12

mysql_list_tables() function

 Displays the list of tables present in the selected database

 Syntax for this function is:

$result = mysql_list_tables(“database_name”,

$link_id);

Where,

 database_name – Specifies the database name

 link_id – Specifies the return value of the database

connection

 result – Stores the result of the function

Trang 13

mysql_num_rows() function

 Shows the number of rows present in the table

 Syntax for this function is:

$num_rows = mysql_num_rows($result);

Where,

 result – Specifies the argument taken for

displaying the number of rows

 num_rows – Stores the result of the function

Trang 14

Closing the connection

 Closes the connection with the MySQL server by using the mysql_close() function

 Syntax for this function is :

mysql_close($link_id);

Where,

 link_id - Specifies the return value of the server connection

Trang 15

Data Access Functions

 Uses MySQL functions in PHP for accessing data

from the tables of the database

 Data are accessed from the tables using the following

mysql functions Those are:

Trang 16

Example for d ata access functions

- I

 For example, to display the records of the table from the

USER database using the data access functions

Trang 17

Example for d ata access functions

die(“Unable to connect to database”);

$sqlquery = mysql_query(“SELECT * FROM

USER_DETAILS WHERE ADDRESS = ‘CALIFORNIA’”);

while($row = mysql_fetch_array($sqlquery))

{

echo “Name:”.$row[USER_NAME];

echo “Phone-No:”.$row[USER_PHONE_NO];

Trang 18

Example for data access functions

Trang 19

Performing SQL queries using

Trang 20

Performing SQL queries using

die(“Unable to connect to database”);

$sql_table=”CREATE TABLE USER_CONTACT(“.“USER_ID

INT NOT NULL PRIMARY KEY,“.“USER_NAME CHAR(25)

NOT NULL,“.“USER_EMAIL_ID CHAR(25)” “)”;

Trang 21

Building HTML tables using SQL

queries

 HTML supports the database application components for accessing the database

 For example, to display all the records of the

user_contact table from the user database by

using HTML table structure

Trang 22

Example for HTML tables using

Trang 23

Example for HTML tables using

SQL queries - II

if($mysql_db)

echo “<BR>Connected to the database”;

echo “<TABLE BORDER BGCOLOR=“WHITE”>”;

echo “<TR><TH> USER_ID <TH><TH> USER_NAME

<TH><TH> USER_EMAIL_ID </TH>”;

echo “<DBQUERY q> select * from user_contact”;

echo “<DBROW><TR><TD><? q.USER_ID></TD><TD><?

q.USER_NAME></TD><TD><? q.USER_EMAIL_ID>

</TD></TR>”;

echo “</DBQUERY>”;echo “</TR>”;echo “</TABLE>”;

?>

Trang 24

 Database APIs allows the developers to write

applications that are movable or easily accessible

between the database products

 Connection with the server is done by using

Ngày đăng: 30/11/2016, 22:11