Simple MySql or MySqli Class Which one of these methods is better/safer to use? And what benefits could I get using one or other?

Simple mysqli:

connection.php

$DBServer = "localhost";
$DBPort = "3306";
$DBUser = "root";
$DBPass = "";
$DBName = "test";

$conn = new mysqli($DBServer, $DBUser, $DBPass, $DBName, $DBPort);
if ($conn-&gt;connect_error) {
    echo "Database connection failed: " . $conn-&gt;connect_error, E_USER_ERROR;
}
mysqli_set_charset($conn,"utf8");


index.php

include_once("connection.php");

$l_name = mysqli_real_escape_string($conn, $_GET['l_name']);

$query = mysqli_query($conn, "SELECT * FROM test WHERE lname='".$l_name."'");
while($row = mysqli_fetch_array($query)){
    echo $row['f_name'].' '.$row['l_name'].'&lt;br&gt;';
}


mysqli with class:

connection.php

class Connect
{

    var $host = 'localhost';
    var $user = 'root';
    var $pass = '';
    var $db = 'test';
    var $con;

    function connect() {
        $con = mysqli_connect($this-&gt;host, $this-&gt;user, $this-&gt;pass, $this-&gt;db);

        if (!$con) {
            //die('Could not connect to database!');
        } else {
            $this-&gt;con = $con; //echo 'Connection established!';
        }
        mysqli_set_charset($this-&gt;con,"utf8");

        return $this-&gt;con;

    }

    function close() {
        mysqli_close($con);
    }

}


index.php

include_once("connection.php");

$con = new Connect();
$con-&gt;connect();

$l_name = mysqli_real_escape_string($con-&gt;con, $_GET['l_name']);

$query = mysqli_query($con-&gt;con, "SELECT * FROM test WHERE lname='".$l_name."'");
while($row = mysqli_fetch_array($query)){
    echo $row['f_name'].' '.$row['l_name'].'&lt;br&gt;';
}


Can you review my code for security and best coding practices?
Don't use either - PDO is much more user-friendly and feature-rich.  Generally a class will be nicer to write code around, and now that we have PDO we don't need to write that class.  The following example is largely stolen from here: https://phpdelusions.net/pdo

$DBUser = "root";
$DBPass = "";
$DBName = "test";
$DBCharset = 'utf8mb4';

$dsn = "mysql:host=$DBServer;dbname=$DBName;charset=$DBCharset";
$DBOptions = [
    PDO::ATTR_ERRMODE            =&gt; PDO::ERRMODE_EXCEPTION,
    PDO::ATTR_DEFAULT_FETCH_MODE =&gt; PDO::FETCH_ASSOC,
    PDO::ATTR_EMULATE_PREPARES   =&gt; false,
];
try {
     $pdo = new PDO($dsn, $DBUser, $DBPass, $DBOptions);
} catch (\PDOException $e) {
     // to avoid a potential credentials leak through a stack trace
     throw new \PDOException($e-&gt;getMessage(), (int)$e-&gt;getCode());
}

$stmt = $pdo-&gt;prepare("SELECT * FROM test WHERE lname = ?");
$stmt-&gt;execute([$_GET['l_name']]);
$data = $stmt-&gt;fetchAll();

foreach ($data as $row) {
    echo "{$row['f_name']} {$row['l_name']}&lt;br&gt;";
}