class for conneting to mariadb in php

To connect to MariaDB, you can use the PDO (PHP Data Objects) or mysqli extension in PHP. Here is an example using PDO:

main.php
<?php
// assuming the following variables are defined
$host = 'localhost';
$dbname = 'my_database';
$user = 'my_username';
$password = 'my_password';

// data source name (DSN) string
$dsn = "mysql:host=$host;dbname=$dbname";

try {
    // create a PDO instance
    $pdo = new PDO($dsn, $user, $password);
    
    // set error mode to exceptions
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
    echo "Connected successfully to $dbname database";
} catch(PDOException $e) {
    echo "Connection failed: " . $e->getMessage();
}
?>
557 chars
23 lines

Make sure to replace the values for $host, $dbname, $user, and $password with your own database connection details.

Using mysqli extension, here's an example:

main.php
<?php
// assuming the following variables are defined
$host = 'localhost';
$dbname = 'my_database';
$user = 'my_username';
$password = 'my_password';

// create a mysqli object
$conn = new mysqli($host, $user, $password, $dbname);

// check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

echo "Connected successfully to $dbname database";
?>
392 chars
18 lines

Hope this helps!

gistlibby LogSnag