PHP 04 mySQL

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


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

PHP and mySQL


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


Enabling MySQL Support in

On UNIX/Linux systems:

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

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


Opening and Closing a MySQL

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


Opening and Closing a MySQL
Connection (continued)

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

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


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

PHP and mySQL


Opening and Closing a MySQL
Connection (continued)

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


Selecting a Database

Select a database with the use database
statement when you log on to the MySQL
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


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


Handling MySQL Errors

Make sure you are using a valid username
and password

Figure 9-2 Database connection error message

PHP and mySQL


Suppressing Errors with the
Error Control Operator

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

PHP and mySQL

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


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
Call the die() and exit() functions as
separate statements or by appending either
function to an expression with the Or operator

PHP and mySQL


Terminating Script Execution
$DBConnect = @mysqli_connect("localhost", "root",
if (!$DBConnect)
die("The database server is not available.");
echo "Successfully connected to the database
$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