PHP 04 mySQL

Topics: MySQL, PHP, SQL Pages: 52 (2601 words) Published: December 4, 2014
Manipulating MySQL
Databases with PHP

Objectives







Connect to MySQL from PHP
Learn how to handle MySQL errors
Execute SQL statements with PHP
Use PHP to work with MySQL databases and
tables
Use PHP to manipulate database records

PHP and mySQL

2

PHP Overview






PHP has the ability to access and manipulate
any database that is ODBC compliant
PHP includes functionality that allows you to
work directly with different types of
databases, without going through ODBC
PHP supports SQLite, database abstraction
layer functions, and PEAR DB

PHP and mySQL

3

Enabling MySQL Support in
PHP


On UNIX/Linux systems:




Configure PHP to use the mysqli extension by
specifying the --with-mysqli parameter when
you run the configure command during
installation

On Windows:




PHP and mySQL

Copy the files libmysql.dll and
php_mysqli.dll to the installation directory
Edit the php.ini configuration file and enable
the extension=php_mysqli.dll directive

4

Opening and Closing a MySQL
Connection






Open a connection to a MySQL database
server with the mysqli_connect() function
The mysqli_connect() function returns a
positive integer if it connects to the database
successfully or false if it does not
Assign the return value from the
mysqli_connect() function to a variable
that you can use to access the database in
your script

PHP and mySQL

5

Opening and Closing a MySQL
Connection (continued)


The syntax for the mysqli_connect()
function is:
$connection = mysqli_connect("host"[, "user ", "password",
"database"])







The host argument specifies the host name
where your MySQL database server is installed
The user and password arguments specify a
MySQL account name and password
The database argument selects a database
with which to work

PHP and mySQL

6

Opening and Closing a MySQL
Connection (continued)
Table 9-1 MySQL server information functions

PHP and mySQL

7

Opening and Closing a MySQL
Connection (continued)

Figure 9-1 MySQLInfo.php in a Web browser
PHP and mySQL

8

Selecting a Database




Select a database with the use database
statement when you log on to the MySQL
Monitor
The syntax for the mysqli_select_db()
function is:
mysqli_select_db(connection, database)



The function returns a value of true if it
successfully selects a database or false if it
does not

PHP and mySQL

9

Handling MySQL Errors


Reasons for not connecting to a database
server include:




PHP and mySQL

The database server is not running
Insufficient privileges to access the data source
Invalid username and/or password

10

Handling MySQL Errors
(continued)


Make sure you are using a valid username
and password

Figure 9-2 Database connection error message

PHP and mySQL

11

Suppressing Errors with the
Error Control Operator




Writing code that anticipates and handles
potential problems is often called
bulletproofing
Bulletproofing techniques include:



PHP and mySQL

Validating submitted form data
Using the error control operator (@) to suppress
error messages

12

Terminating Script Execution








The die() and exit() functions terminate
script execution
The die() version is usually used when
attempting to access a data source
Both functions accept a single string
argument
Call the die() and exit() functions as
separate statements or by appending either
function to an expression with the Or operator

PHP and mySQL

13

Terminating Script Execution
(continued)
$DBConnect = @mysqli_connect("localhost", "root",
"paris");
if (!$DBConnect)
die("The database server is not available.");
echo "Successfully connected to the database
server.";
$DBSelect = @mysqli_select_db($DBConnect, "flightlog");
if (!$DBSelect)
die("The database is not...
Continue Reading

Please join StudyMode to read the full document

You May Also Find These Documents Helpful

  • Essay on PHP with MySQL
  • Php Mysql Essay
  • Mysql Essay
  • Mysql Essay
  • 04 Essay
  • a guide to mysql ch 7 Essay
  • mysql Essay
  • Essay about How to Install Apache Mysql Php Phpmyadmin

Become a StudyMode Member

Sign Up - It's Free