This document provides an overview of connecting to and interacting with databases in PHP using various approaches like MySQLi, PDO, and database-specific extensions. It discusses configuration of common frameworks like CodeIgniter, CakePHP, and Zend. It also covers concepts like prepared statements, stored procedures, and classes for MySQLi, PDO, and related extensions.
1. PHP, PHP FRAMEWORKS AND DATABASE
Sayed Ahmed
Computer Engineering, BUET, Bangladesh
MSC, Computer Science, U of Manitoba, Canada
http://www.JustEtc.net
http://sayed.justetc.net
sayed@justetc.net
2. TOPICS
ο Connection strategies
ο Configuration for connection in different
frameworks
ο PHP and DB2, MSSQL Server
ο Common database oriented operations
ο Reference to DB oriented functions/classes
3. CONNECTION
ο mysql_connect
ο mysqli_connect
ο PDO
ο PDO_ODBC
ο Connection from Frameworks
ο Code Igniter β configure for db connection
ο CakePHP - configure for db connection
ο Zend - configure for db connection
4. PDO VS MYSQLI
ο Overall, PDO is a better choice than mysqli
ο However, if performance is the only issue
that you care, and
ο mysql will be the backend database all the
time,
ο you can use mysqli
ο Reference:
ο http://net.tutsplus.com/tutorials/php/pdo-vs-
mysqli-which-should-you-use/
5. MYSQL_CONNECT
ο mysql_connect
ο Opens a connection to a MySQL Server
ο Deprecated as of PHP 5.5.0, and will be removed in
the future.
ο Instead use MySQLi or PDO_MySQL
ο mysql_connect examples
16. CAKEPHP AND MYSQL
ο A copy of CakePHPβs database configuration
file is found in
ο /app/Config/database.php.default
ο Make a copy of this file in the same directory,
but name it
ο database.php
ο Change database.php as follows
27. PHP AND IBM DB2
ο PHP has concepts like PDO and PDO_ODBC, and
generic odbc features that will allow you to connect to and
work with DB-2 databases. Generic odbc is the
general/procedural way to connect to. PDO and
PDO_ODBC are object oriented ways. PDO works with
native IBM drivers. PDO_ODBC uses odbc connections
to the DB2. For PHP you need to install drivers to support
DB2. I worked with PDO_ODBC to connect to DB-2
databases in IBM iSeries servers. You need to install
odbc db2 drivers for iseries servers. Then in
/etc/odbcinst.ini file you need to mention driver
specifications and in /etc/odbc.ini, you need to mention
the odbc dsn name, database name, and some other
parameters to connect to the database. Though, you may
also supply some parameters in the PDO_ODBC connect
method.
33. MYSQLI β THE MYSQLI CLASS
ο mysqli::$affected_rows β Gets the number of affected
rows in a previous MySQL operation
ο mysqli::autocommit β Turns on or off auto-committing
database modifications
ο mysqli::begin_transaction β Starts a transaction
ο mysqli::change_user β Changes the user of the specified
database connection
ο mysqli::character_set_name β Returns the default
character set for the database connection
ο mysqli::$client_info β Get MySQL client info
ο mysqli::$client_version β Returns the MySQL client
version as a stringmysqli::close β Closes a previously
opened database connection
34. MYSQLI β THE MYSQLI CLASS
ο mysqli::commit β Commits the current transaction
ο mysqli::$connect_errno β Returns the error code from
last connect call
ο mysqli::$connect_error β Returns a string description of
the last connect error
ο mysqli::__construct β Open a new connection to the
MySQL server
ο mysqli::debug β Performs debugging operations
ο mysqli::dump_debug_info β Dump debugging
information into the log
ο mysqli::$errno β Returns the error code for the most
recent function call
35. MYSQLI β THE MYSQLI CLASS
ο mysqli::$error_list β Returns a list of errors from the last command executed
ο mysqli::$error β Returns a string description of the last error
ο mysqli::$field_count β Returns the number of columns for the most recent query
ο mysqli::get_charset β Returns a character set objectmysqli::get_client_info β
Get MySQL client info
ο mysqli_get_client_stats β Returns client per-process statistics
ο mysqli_get_client_version β Returns the MySQL client version as an integer
ο mysqli::get_connection_stats β Returns statistics about the client connection
ο mysqli::$host_info β Returns a string representing the type of connection used
ο mysqli::$protocol_version β Returns the version of the MySQL protocol used
ο mysqli::$server_info β Returns the version of the MySQL server
ο mysqli::$server_version β Returns the version of the MySQL server as an
integer
ο mysqli::get_warnings β Get result of SHOW WARNINGS
36. MYSQLI β THE MYSQLI CLASS
ο mysqli::$info β Retrieves information about the most recently
executed querymysqli::init β Initializes MySQLi and returns a
resource for use with mysqli_real_connect()mysqli::$insert_id β
Returns the auto generated id used in the last querymysqli::kill β
Asks the server to kill a MySQL threadmysqli::more_results β
Check if there are any more query results from a multi
querymysqli::multi_query β Performs a query on the
databasemysqli::next_result β Prepare next result from
multi_querymysqli::options β Set optionsmysqli::ping β Pings a
server connection, or tries to reconnect if the connection has
gone downmysqli::poll β Poll connectionsmysqli::prepare β
Prepare an SQL statement for executionmysqli::query β
Performs a query on the databasemysqli::real_connect β Opens
a connection to a mysql servermysqli::real_escape_string β
Escapes special characters in a string for use in an SQL
statement, taking into account the current charset of the
connectionmysqli::real_query β Execute an SQL query
37. MYSQLI_STMT β THE MYSQLI_STMT CLASS
ο mysqli_stmt::$affected_rows β Returns the total number
of rows changed, deleted, or inserted by the last executed
statementmysqli_stmt::attr_get β Used to get the current
value of a statement attributemysqli_stmt::attr_set β
Used to modify the behavior of a prepared
statementmysqli_stmt::bind_param β Binds variables to
a prepared statement as
parametersmysqli_stmt::bind_result β Binds variables to
a prepared statement for result storagemysqli_stmt::close
β Closes a prepared statementmysqli_stmt::data_seek
β Seeks to an arbitrary row in statement result
setmysqli_stmt::$errno β Returns the error code for the
most recent statement callmysqli_stmt::$error_list β
Returns a list of errors from the last statement executed
38. MYSQLI_RESULT β THE MYSQLI_RESULT CLASS
ο mysqli_result::$current_field β Get current field offset of a result
pointermysqli_result::data_seek β Adjusts the result pointer to an
arbitrary row in the resultmysqli_result::fetch_all β Fetches all
result rows as an associative array, a numeric array, or
bothmysqli_result::fetch_array β Fetch a result row as an
associative, a numeric array, or bothmysqli_result::fetch_assoc β
Fetch a result row as an associative
arraymysqli_result::fetch_field_direct β Fetch meta-data for a
single fieldmysqli_result::fetch_field β Returns the next field in
the result setmysqli_result::fetch_fields β Returns an array of
objects representing the fields in a result
setmysqli_result::fetch_object β Returns the current row of a
result set as an objectmysqli_result::fetch_row β Get a result row
as an enumerated array
39. MYSQLI_DRIVER, MYSQLI_WARNING
ο mysqli_driver β The mysqli_driver
classmysqli_driver::embedded_server_end β Stop
embedded server
ο mysqli_driver::embedded_server_start β Initialize
and start embedded server
ο mysqli_driver::$report_mode β Enables or disables
internal report functions
ο mysqli_warning β The mysqli_warning
classmysqli_warning::__construct β The __construct
purpose
ο mysqli_warning::next β The next purpose
ο mysqli_sql_exception β The mysqli_sql_exception
class
40. PDO β THE PDO CLASS
ο PDO::beginTransaction β Initiates a
transactionPDO::commit β Commits a
transactionPDO::__construct β Creates a PDO
instance representing a connection to a
databasePDO::errorCode β Fetch the
SQLSTATE associated with the last operation
on the database handlePDO::errorInfo β Fetch
extended error information associated with the
last operation on the database
handlePDO::exec β Execute an SQL statement
and return the number of affected
rowsPDO::getAttribute β Retrieve a database
connection attribute
41. PDOSTATEMENT β THE PDOSTATEMENT CLASS
ο PDOStatement::bindColumn β Bind a column to a PHP
variablePDOStatement::bindParam β Binds a parameter
to the specified variable namePDOStatement::bindValue
β Binds a value to a
parameterPDOStatement::closeCursor β Closes the
cursor, enabling the statement to be executed
again.PDOStatement::columnCount β Returns the
number of columns in the result
setPDOStatement::debugDumpParams β Dump an SQL
prepared commandPDOStatement::errorCode β Fetch
the SQLSTATE associated with the last operation on the
statement handlePDOStatement::errorInfo β Fetch
extended error information associated with the last
operation on the statement handle
42. PDOEXCEPTION, MISC
ο PDOException β The PDOException class
ο PDO DriversCUBRID (PDO) β CUBRID
Functions (PDO_CUBRID)
ο MS SQL Server (PDO) β Microsoft SQL
Server and Sybase Functions (PDO_DBLIB)
ο Firebird/Interbase (PDO) β
Firebird/Interbase Functions
(PDO_FIREBIRD)
ο IBM (PDO) β IBM Functions (PDO_IBM)