Connecting to MariaDB

Chaandgus
Posts: 4
Joined: Mon Aug 30, 2021 4:00 am

Re: Connecting to MariaDB

Post by Chaandgus »

Hello Steven, forgive me for re-digging the subject but I need skills that are yours...if you accept the challenge.

I had to reinstall PHPMyAdmin and MariaDB.

I have created my database and my user. I can connect to it using SSH

Code: Select all

mysql -u user -p database
However, I get a blank page when I use the following method:

Code: Select all

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

   <body>
      <?php
         $host = 'host:3307'; //host name
         $name= 'database'; //Name of the data base
         $user = 'user'; //user
         $pass = 'password';

         $conn = mysqli_connect($host, $user, $pass);
      
         if(! $conn ) {
            die('Could not connect: ' . mysqli_error());
    
         }
         
         echo 'Connected successfully';
         mysqli_close($conn);
      ?>
   </body>
</html>
Could you give me an advise please ?

My sockets are /run/mysqld/mysqld.sock in MariaDB and /run/mysqld/mysqld.sock in PHPMyAdmin.

excepted that, I really doesn't know what should be the trouble.
steven
Posts: 136
Joined: Sun Oct 01, 2017 3:08 pm

Re: Connecting to MariaDB

Post by steven »

If you you are using the code exactly as shown in your post, you are closing the database connection before you do anything. The connection must remain open when getting or writing data. If PHP closes the connection before reading database data to variables or arrays, nothing happens.
steven
Posts: 136
Joined: Sun Oct 01, 2017 3:08 pm

Re: Connecting to MariaDB

Post by steven »

Sorry for the really slow response but I was very busy and did not have time to test your code, which does not connect. When I didn't receive a reply I forgot about it. While rechecking some posts I noticed you did not reply so I thought I would post this code anyway.

Try using this:

Code: Select all

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

   <body>
      <?php
         mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); // Added to report connection errors
         $host = 'localhost'; //host name
         $name = 'database_name'; // Name of the data base
         $user = 'database_user'; // User
         $pass = 'passsword'; // Password
         $port = null;
         $socket = '/run/mysqld/mysqld.sock';

         $conn = mysqli_connect($host,$user,$pass,$name,$port,$socket);
      
         if(! $conn ) {
            die('Could not connect: ' . mysqli_error());
    
         }
         
         echo 'Connected successfully';
         mysqli_close($conn);
      ?>
   </body>
</html>
Post Reply