ГлавнаяMySQL Database Handling In PHP
MySQL Database Handling In PHP

MySQL Database Handling In PHP

Most interactive websites nowadays require knowledge to be offered dynamically and interactively based on enter from the user. For example, a buyer could have to log into a retail web site to check his buying history. On this instance, the web site would have stored varieties of data to ensure that the client to perform the check - the client's personal login details; and the shopper's bought items. This information could be stored in types of storage - flat recordsdata or databases.

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

The shortcoming to index the data. This makes it essential to potentially read ALL the data sequentially. This can be a major downside if there are a number of records in the flat file because the time required to read the flat file is proportionate to the number of records within the flat file.
The lack to effectively management ms access to mysql by users to the data
The inefficient storage of the data. In most cases, the data would not be encrypted or compressed as this would exacerbate the problem no. 1 above
The alternative which is, for my part, the only possible methodology, 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 additionally present a suite of accompanying utilities that permit the database administrator to maintain the database - for instance, backup and restore, etc.

Websites scripted utilizing PHP are very properly suited for the MySQL database as PHP has a custom and integrated MySQL module that communicates very efficiently with MySQL. PHP may communicate with MySQL via the usual ODBC as MySQL is ODBC-compliant, Nevertheless, this will not be as environment friendly as utilizing the customized MySQL module for PHP.

The rest of this article is a tutorial on how to use PHP to:

Connect to a MySQL database
Execute normal SQL statements against the MySQL database
Beginning a Session with MySQL

Before the PHP script can communicate with the database to question, insert or update the database, the PHP script will first need to connect to the MySQL server and specify which database within the MySQL server to operate on.

The mysql_connect() and mysql_select_db() functions are supplied for this purpose. With a view to hook up with the MySQL server, the server name/address; a consumername; and a sound password is required. Once a connection is profitable, the database needs to be specified.

The next 2 code excerpts illustrate the right way to perform the server connection and database selection:

@mysql_connect("[servername]", "[username]", "[password]") or die("Can not 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() features might produce if an error happenred. The die() function is used to end the script execution and display a customized error message.

Executing SQL Statements in opposition to a MySQL database

Once the connection and database choice is efficiently carried out, the PHP script can now proceed to operate on the database utilizing commonplace SQL statements. The mysql_query() perform is used for executing normal SQL statements towards the database. In the following instance, the PHP script queries a table called tbl_login in the beforehand chosen database to determine if a personname/password pair offered by the person is valid. Адрес сайта: