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 u0026 Base Station to Steam VR 2024, December
Anonim

To connect an application to a database, it must send the appropriate sequence of commands in the language used by this particular DBMS. The MySQL database management system is very often used today, and applications for working with them are written in a scripting server-side programming language PHP. Below is a variant of the sequence of commands in this language for connecting an application to a MySQL database.

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

Instructions

Step 1

Use PHP's built-in mysql_connect function to send a database connection request to the SQL server. This function has three required parameters, the first of which must specify the address of the database. Most often, this server and the script accessing it are on the same physical server, so the localhost reserved word is used as the address. The second parameter should contain the login of the connecting user, and the third should contain his password. For example:

$ DBconnection = mysql_connect ("localhost", "myName", "myPass");

Step 2

Apply the built-in mysql_select_db function after creating a new connection to the SQL server. This function selects one of the databases located on the server for subsequent work with the tables placed in it. You need to pass two variables to the function: the first should contain the name of the required database, and the second should contain the resource link that you created in the previous step. For example:

mysql_select_db ("myBase", $ DBconnection);

Step 3

Sometimes the encoding used by the application when displaying data does not match the encoding in which the information is written in the database tables. In this case, you need to give the server the setting in which encoding it should receive your requests and in which encoding it should convert its responses. This can be done by sending, after selecting a database, for example, the following set of SQL queries:

mysql_query ("SET character_set_client = 'cp1251'");

mysql_query ("SET character_set_results = 'cp1251'");

mysql_query ("SET collation_connection = 'cp1251_general_ci'");

After that, you can start working directly with the database tables.

Step 4

Use function and class libraries specially designed for connecting to PHP applications as a medium between your scripts and the database. The advantage of using them is that all the nuances associated with data exchange in such libraries are taken into account and carefully debugged. Their use helps to avoid accidental errors, simplify writing scripts for working with databases and make them more versatile. An example of such a library is DbSimple, developed under the leadership of Dmitry Koterov.

Recommended: