Last Updated on: Saturday, February 7, 2009 5:50 PM

WDS 4 - PHP database interaction functions

Using a database controlled website can provide dynamic content that is easy to keep organized. The data in the database can be changed without having any HTML skills and knowledge. Using PHP to control and communicate with the database the website can be maintained through a designed control panel. This would allow management of the site through a web page only accessible through a login which would also be controlled by the database. PHP controls the database through the use of functions, variables, constants, expressions, and operators.

Generally database interaction in PHP is performed with one of the database-specific interface functions (e.g. mysql_*(), pgsql_*(), mysqli_*()). In PHP 5 mysqli was introduced which allowed for a new object-oriented interface.

PHP interacts with a database by first connecting to it. If the connection is successful then functions can be performed. These functions work by performing queries on the database, the database then returns a result which PHP fetches. The result could be the end result desired or it could be used to complete other functions.

PHP can be used to perform either user defined functions or common list of pre-defined functions built into PHP. User defined functions are built by the user and do not need to be defined before they are referenced, except when the function is conditionally defined. If the function is only run when a certain condition or conditions are met then the function must be defined earlier in the code.

Some of the common built in functions are string and variable. These functions are used extremely often and it why they were built into PHP. When using the built in functions using parameters that are different from what the function expects, the value returned will often be null. Built in functions must be used as expected to get valid results.

A working example of his principle can be found here. The example is a PHP script connecting to a database and returning a echo value of “It worked” to show that the connection was made to the database. The connecting command was done using “mysqli”