skip to Main Content

I would like to establish a connection to my MYSQL database. Unfortunately, I haven’t found the right one for me anywhere. Does anyone have a solution that is similar to this one?

I have tried this but i want to have utf 8 to charset and it does not work

`
`    <?php
$servername = "localhost";
$username = "u123456789_User";
$password = "MyStr0ngPa$!";

$conn = mysqli_connect($servername, $username, $password);

if (!$conn) {

    die("Connection failed: " . mysqli_connect_error());

}
echo "Connected successfully";
mysqli_close($conn);
?>
`
`

2

Answers


  1. try it with:

    <?php
    $server = 'localhost:3306';
    //or localhost: 127.0.0.1:3306
    $username = 'root';
    $password = '';
    $schema = '';
    
    try{
        $conn = new PDO('mysql:host='.$server.';dbname='.$schema.';charset=utf8',$user, $password);
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    }
    catch(Exception $e){
        echo "Connection failed: " . $e->getMessage();
    }
    ?>
    
    Login or Signup to reply.
  2. To use UTF-8, set the client character set using mysqli_set_charset once the connection has been established.

    mysqli_set_charset($conn, 'utf8mb4');
    

    The utf8mb4 uses UTF-8 encoding using one to four bytes per character.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search