G
G
GUUDHS2018-08-25 16:41:56
PHP
GUUDHS, 2018-08-25 16:41:56

How to connect to MySQL via PHP 7 on Localhost ("Connection failed: could not find driver" error)?

I installed Apache, PHP 7 on Windows, MySQL was installed, I did not install it. Made basic settings in httpd.conf and php.ini. The extensions in php.ini are all uncommented
The code used is the following. On the hosting https://www.000webhost.com it connects successfully, on the local
host "Connection failed: could not find driver" :

<?php
$servername = "localhost";
$username = "root";
$password = "2523423";
$database = "test1";

try {
    $conn = new PDO("mysql:host=$servername;dbname=$database", $username, $password);
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    echo "Connected successfully"; 
    } catch(PDOException $e) {    
    echo "Connection failed: " . $e->getMessage();
    }
?>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
Pavel, 2018-08-25
@PavelMonro

Are you sure it costs php7.x-mysql?
Check in php.ini extension

S
Super Star, 2018-08-25
@3dben

Why a password? Usually root without a password =)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question