How to be connected to base

How to be connected to base

Digital Hits: 10

MySQL is the database management system which is most widely used today in web construction. Even more actively during creation of Internet resources the server scripting PHP programming language is applied and, of course, the whole feature set for work with MySQL is provided in it. There are among them and those which in PHP scripts are used for connection to the database.


1. Create the new variable in a PHP script and assign it the link returned by the embedded function of mysql_connect. This function needs to transfer three parameters: sql-server address, login and user password. The full link which is beginning with the indication of the protocol of connection and coming to an end with the port number of a remote server - for example, can be the address.

2. If the script is executed on the same local server where also MySQL DBMS is placed, then instead of the full address enter the backed-up designation localhost. For example, the line containing the new variable to which the identifier of the link returned by this function is assigned can look so:

$connectToDB = mysql_connect (""localhost"" "", MySQLuserName" """, MySQLuserPass"");

If connection is not established, then the variable of $connectToDB will matter False.

3. On the previous step you came into contact with the SQL server, and after that it is necessary to send a request for the choice of one of the databases available to the user which login you transferred the mysql_connect functions. For this purpose use other embedded function of the PHP language - mysql_select_db. She requires the obligatory indication of two parameters - names of the database interesting you and the link to the established connection with the SQL server. For example, if tables necessary to you are placed in base with the name SiteBase, then for connection from the previous step the call of this function should be written so:

mysql_select_db (""SiteBase"", $connectToDB);

4. Not always the encoding of tables of the database matches the coding used by the web application therefore it is desirable for SQL server to give right after the choice of base exact instructions in which coding he will receive and send information to the web application and in which it should register and be read out from tables of the database. For this purpose use the embedded function of mysql_query, transferring it the necessary MySQL-commands. It is enough to send set of three such commands, for example:

mysql_query (""SET character_set_client='cp1251'"");
mysql_query (""SET character_set_results='cp1251'"");
mysql_query (""SET collation_connection='cp1251_general_ci'"");

Author: ┬źMirrorInfo┬╗ Dream Team