MySQL Database Handling In PHP

MySQL Database Handling In PHP

Most interactive web sites nowadays require data to be offered dynamically and interactively based on enter from the user. For instance, a customer might must log right into a retail web site to check his buying history. On this occasion, the website would have stored forms of information to ensure that the customer to perform the check - the customer's personal login details; and the shopper's purchased items. This information will be stored in kinds of storage - flat files or databases.

Flat information are only possible in very low to low volume websites as flat recordsdata have 3 inherent weaknesses:

The lack to index the data. This makes it necessary to potentially read ALL the info sequentially. It is a major drawback if there are a number of records within the flat file because the time required to read the flat file is proportionate to the number of records in the flat file.
The shortcoming to efficiently management ms access to mysql by customers to the data
The inefficient storage of the data. In most cases, the info would not be encrypted or compressed as this might exacerbate the issue no. 1 above
The alternative which is, in my view, the one possible method, is to retailer the information in a database. One of the prevalent databases in use is MySQL. Data that's stored in a database can simply be indexed, managed and stored efficiently. Besides that, most databases also provide a suite of accompanying utilities that permit the database administrator to maintain the database - for example, backup and restore, etc.

Websites scripted utilizing PHP are very well suited for the MySQL database as PHP has a custom and integrated MySQL module that communicates very efficiently with MySQL. PHP can even communicate with MySQL by means of the standard ODBC as MySQL is ODBC-compliant, Nevertheless, this won't be as environment friendly as using the custom MySQL module for PHP.

The rest of this text is a tutorial on the way to use PHP to:

Hook up with a MySQL database
Execute standard SQL statements in opposition to the MySQL database
Starting a Session with MySQL

Earlier than the PHP script can communicate with the database to question, insert or update the database, the PHP script will first want to hook up with the MySQL server and specify which database in the MySQL server to operate on.

The mysql_connect() and mysql_select_db() functions are offered for this purpose. In an effort to hook up with the MySQL server, the server name/address; a username; and a legitimate password is required. As soon as a connection is profitable, the database must be specified.

The next 2 code excerpts illustrate learn how to perform the server connection and database selection:

@mysql_connect("[servername]", "[username]", "[password]") or die("Cannot connect to DB!");

@mysql_select_db("[databasename]") or die("Cannot select DB!");

The @ operator is used to suppress any error messages that mysql_connect() and mysql_select_db() capabilities may produce if an error happenred. The die() function is used to end the script execution and display a customized error message.

Executing SQL Statements towards a MySQL database

As soon as the connection and database selection is efficiently carried out, the PHP script can now proceed to operate on the database utilizing normal SQL statements. The mysql_query() function is used for executing normal SQL statements in opposition to the database. Within the following instance, the PHP script queries a table called tbl_login in the beforehand chosen database to determine if a username/password pair provided by the consumer is valid.