How To Connect To The Base

Table of contents:

How To Connect To The Base
How To Connect To The Base

Video: How To Connect To The Base

Video: How To Connect To The Base
Video: VP: How to Connect HTC Vive Tracker & Base Station to Steam VR 2024, November
Anonim

MySQL is the most widely used database management system in web construction today. The server-side scripting language PHP is even more actively used when creating Internet resources and, of course, it provides a whole set of functions for working with MySQL. Among them, there are those that are used in PHP scripts to connect to the database.

How to connect to the base
How to connect to the base

Instructions

Step 1

Create a new variable in the php script and assign it the link returned by the built-in mysql_connect function. This function must be passed three parameters: sql-server address, username and password. The address can be a full link starting with the connection protocol and ending with the port number of the remote server - for example,

Step 2

If the script is executed on the same local server where the MySQL DBMS is located, then instead of the full address, enter the reserved designation localhost. For example, a string containing a new variable that is assigned the reference identifier returned by this function might look like this:

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

If the connection fails, the $ connectToDB variable will be False.

Step 3

In the previous step, you established contact with the SQL server, and after that you need to send a request to select one of the databases available to the user whose login you passed to the mysql_connect function. To do this, use another built-in PHP function - mysql_select_db. It requires the mandatory indication of two parameters - the name of the database you are interested in and a link to the established connection to the SQL server. For example, if the tables you need are located in a database called SiteBase, then to connect from the previous step, the call to this function must be written as follows:

mysql_select_db ("SiteBase", $ connectToDB);

Step 4

The encoding of the database tables does not always coincide with the encoding used by the web application, so it is advisable to immediately after selecting the database give the SQL server precise instructions in which encoding it will receive and send information to the web application, and in which it should be written and read from database tables. To do this, use the built-in mysql_query function, passing in the required MySQL commands. It is enough to send a 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'");

Recommended: