LocaHost.NET
Tools

PHP MySQL How to connect to the MySQL database server?


The connection to the MySQL database, we can accomplish a number of ways - like most things in PHP.
It may be:
- Normal call declared in PHP,
- More complex function of the task will be to connect to the database,
- Defined in a separate file that you attach to the performance of the code (function include, or require).
- Etc.

Below I will present several ways to connect to a MySQL database.

NOTE:
The following examples show how to connect to a MySQL database (this is the default way in php).
If you want to create a connection with two or more MySQL databases at the same time, I invite you to a separate article.

EXAMPLE 1:
Simple PHP code that creates direct connection to a MySQL database

<?php
# 1. At the beginning we create a connection to the host the MySQL database.
$host = 'locahost';
$user = 'user';
$pass = 'pass';
$con = @mysql_connect($host, $user, $pass);
if(!
$con) {
        echo
"ERROR MySQL: Connect to Server\n";
        exit;
}
# 2. Connect to the specified database.
$db = 'baza_danych_MySQL';
@
mysql_select_db($db, $con);
# 3. At the end of the close connection to the MySQL database - optional.
mysql_close($con);
?>


NOTES:
1. Using the @ sign (monkeys) the functions, this causes the error does not appear in the event of failure of implementation functions.
In this case, the function mysql_connect could display an error if the host such a database was unreachable, and the function mysql_select_db () wyświetliłaby error if unable to connect to a specific database.
Using @ mysql_connect () instead of mysql_connect () fails to display unwanted messages.

2. In the mysql_select_db function, we used the call to the current connection $ con: mysql_select_db ($ db, $ con);
But the second argument is unnecessary, we can simply use the mysql_select_db ($ db);

EXAMPLE 2:
The following PHP code in a more sophisticated functions to connect to the MySQL database.
Function:
- Connects to host the MySQL database,
- Connects us to the selected MySQL database,
- Sets the encoding selected in this case UTF-8 (call, client, results returned, etc.)
- Returns the appropriate message in case of failure (connection, database setup, setting the correct encoding)
- Returns the connection handle,
- Ideal for developerki, we always know when something has gone wrong!

<?php
function connect_to_db($host, $user, $pass, $db) {
        if(!(
$con = @mysql_connect($host, $user, $pass))) {
                echo
"ERROR MySQL: Connect to Server\n";
                exit;
        }
        if(!
mysql_select_db($db, $con)) {
                echo
"ERROR MySQL: Connect to DataBase\n";
                exit;
        }
        if(!
mysql_query("SET NAMES 'utf8'", $con)) {
                echo
"ERROR MySQL: SET NAMES 'utf8'\n";
                exit;
        }
        if(!
mysql_query("SET CHARACTER SET 'utf8'", $con)) {
                echo
"ERROR MySQL: SET CHARACTER SET 'utf8'\n";
                exit;
        }
        if(!
mysql_query("SET character_set_client = 'utf8'", $con)) {
                echo
"ERROR MySQL: SET character_set_client = 'utf8'\n";
                exit;
        }
        if(!
mysql_query("SET character_set_results = 'utf8'", $con)) {
                echo
"ERROR MySQL: SET character_set_results = 'utf8'\n";
                exit;
        }
        if(!
mysql_query("SET character_set_connection = 'utf8'", $con)) {
                echo
"ERROR MySQL: SET character_set_connection = 'utf8'\n";
                exit;
        }
        
mb_internal_encoding("UTF-8");
        
mb_regex_encoding("UTF-8");
        return
$con;
}
# Call a function call to the MySQL database
$con = connect_to_db('locahost', 'user', 'pass', 'db');
# [...]
# Close connection to the database - optional
mysql_close($con);
?>


Artykuł utworzony: 2020-03-08 02:01:01: MySQL, PHP, mysql_connect(), mysql_select_db(), mysql_close(), SET character_set_results, SET character_set_connection, SET character_set_client, SET NAMES, SET CHARACTER SET, Function to connect to the MySQL database, Connection to the MySQL database, locahost, SET CHARACTER SET utf8.

» Regulamin

» Kontakt

MySQLPHPPhpMyAdminlocalhost127.0.0.1 © LocaHost.NET