In this article, we will learn how we can access the data from the MySQL database using the PHP language.

What is MySQL connection string

Connection string is a string that specifies the information related to database login. For understanding, we will consider an example of a PHP scripting language and will write a code to establish the connection with the database. We have a database, named “Organization”, the user name is “maadi” and password is “qwer1234”:

In PHP, we have different MySQL built-in functions which make our task easy, like we have a connection string known as “mysqli_connect” which is used to connect the database. The general syntax of the mysqli_connect() function is:

[Variable_name (in which results to be stored)] = mysqli_connect ([host],[user_name],[password],[database_name]);

In our code, this statement is used as:

$connection = mysqli_connect(“localhost”,“maadi”,“qwer1234”,“Organization”);

Then we have another built-in function, mysqli_connect_errno(), which is used to return the error if the connection failed with the database and in the last, we use another built-in function, mysqli_close() which is used to close the connection. For more understanding, we will run the following code which is stored in a file, named, file.php:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/What-is-connection-string-in-MySQL-03.png" data-lazy- height="380" src="data:image/svg xml,” width=”904″>

Open the terminal and type:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/what-is-connection-string-mysql-01.png" data-lazy- height="144" src="data:image/svg xml,” width=”660″>

Our connection with the database is connected successfully as the output is displaying. Now we will enter the wrong password and will execute the code again:

The file is as:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/What-is-connection-string-in-MySQL-04.png" data-lazy- height="360" src="data:image/svg xml,” width=”904″>

Again run the command in a terminal:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/What-is-connection-string-in-MySQL-02.png" data-lazy- height="186" src="data:image/svg xml,” width=”900″>

The output generated the error of the password.

Conclusion

The MySQL connection string is used to open a new connection with the MySQL database, by using different parameters and is used in different scripting languages like PHP, Python, and C language. The PHP language is used a lot nowadays in backend development so we have discussed in this article the use of MySQL connection string with the help of a PHP language code.

About the author

<img data-del="avatar" data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/hammad–150×150.jpg616b81f28e4fc.jpg" height="112" src="data:image/svg xml,” width=”112″>

Hammad Zahid

I’m an Engineering graduate and my passion for IT has brought me to Linux. Now here I’m learning and sharing my knowledge with the world.