MySQL and PHP

Standard Connections
The following script creates a MySQL resource,, that can be used to make queries:



It is a good idea to put this script into a separate file, and then use PHP's  function to append it to the beginning of any web page that requires database access. There is normally no reason to have more than one database for a website (particularly because there is no theoretical limit to the number of tables per database), so there is rarely a need for more than one connection resource. The connection will close once execution of the script ends, or by invoking the  function.

Persistent Connections
An alternative to the normal connection is a persistent connection. first searches for an existing connection that has the same hostname, username, and password. If the function finds one, it returns its identifier instead of trying to open a new connection. The persistent connection will not end with execution of the script - nor will it respect the  function.

Retrieving a Resultset
Given this example table : ++---+ ++---+ ++---+ This example script will extract all the data in the table and display it in an HTML table, assuming the connection resource is stored in the  variable: 