MariaDB establish a connection

One way to establish a connection with MariaDB is a binary file using mysql at the command prompt.

MySQL script

See the examples given below.

[root@host]# mysql -u root -p
Enter password:***

Code MariaDB given above and connected to a command prompt to execute SQL commands. After entering the code, you will see a welcome message, indicating a successful connection, and displays the version number.

Welcome to the MariaDB monitor. Commands end with ; or g.
Your MariaDB connection id is 122323232
Server version: 5.5.40-MariaDB-log

Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.  
mysql>

This example uses the root access, but of course, any user with access rights and prompt MariaDB perform operations.

Disconnect command via the exit MariaDB as follows -

mysql> exit

Connect PHP script

Another way to connect to and disconnect MariaDB is PHP scripting. PHP provides for opening the database connection mysql_connect()function.
It uses five optional parameter and returns a link identifier MariaDB after a successful connection, or return false on the connection failed. It also provides for closing the database connection
mysql_close()function, which uses a single parameter.

grammar

Review the following connection PHP script syntax

connection mysql_connect(server,user,passwd,new_link,client_flag);

Parameters described below

No. Parameters and Descriptions
1 server This optional parameter specifies the host name of the database server is running. The default value "localhost: .3036".
2 user This optional parameter specifies the user name to access the database. The default value is the owner of the server.
3 passwd This optional parameter specifies the user's password. The default value is empty.
4 new_link This optional parameter specifies the second call with the same arguments mysql_connect () when, instead of a new connection, returns the identifier of the current connection.
5 client flags - flag clients This optional parameter in combination with the following constant value

  • MYSQL_CLIENT_SSL - it uses ssl encryption.
  • MYSQL_CLIENT_COMPRESS - it uses compression protocol.
  • MYSQL_CLIENT_IGNORE_SPACE - it allows space after the function name.
  • MYSQL_CLIENT_INTERACTIVE - it allows interactive before closing the connection timeout in seconds of inactivity.

Please see below given off PHP script syntax -

bool mysql_close ( resource $link_identifier );

If you omit the resource, the recently opened resource will be closed. It returns true if successfully closed or false.

The following sample code attempts to connect the server MariaDB -

<html>
   <head>
      <title>Connect to MariaDB Server</title>
   </head>

   <body>
      <?php
         $dbhost = 'localhost:3036';
         $dbuser = 'guest1';
         $dbpass = 'guest1a';
         $conn = mysql_connect($dbhost, $dbuser, $dbpass);

         if(! $conn ) {
            die('Could not connect: ' . mysql_error());
         }

         echo 'Connected successfully';
         mysql_close($conn);
      ?>
   </body>
</html>

After a successful connection, you will see the following output

mysql> Connected successfully

This switched: http: //codingdict.com/article/7096

Guess you like

Origin www.cnblogs.com/bczd/p/12009703.html