1

Trying to connect to a remote mysql server with php inserted into html.

I'm getting an error:

could not find driver with the below code.

The mysql example in the link does not return anything.

What I want is to extract and save info plus edit, create tables through http. How can I do that?

MySql version: 5.7.22-0ubuntu18.04.1
Php version: PHP Version 7.2.3-1ubuntu1

Source: php pdo code

<?php
    //Database Credentials
    $host = 'localhost';
    $database = 'dbName';
    $username = 'userName';
    $password = 'userPassword';

    try {
      $DBH = new PDO("mysql:host=$host;dbname=$database", $username, $password); }
    catch(PDOException $e) {
        echo "did catch that! ";
        echo $e->getMessage(); }

    echo " #it works";

?>

Edit note: Got it working after following Thomas Deutschländer advice. Server is local and php is running on server, are going to view results/return from php in web page from local and remote net.

Some info: Gained (viewed from php -m) after sudo apt-get install php-mysql:
mysqli
mysqlnd
pdo_mysql
(viewed from dpkg --get-selections | grep php)
php-mysql
php7.2-mysql
(viewed from dpkg --get-selections | grep mysql)
php-mysql
php7.2-mysql

Had previously:
PDO - viewed from php -m

TeaWave
  • 105
  • 1
  • 2
  • 7
  • "Note that you cannot perform any database functions using the PDO extension by itself; you must use a database-specific PDO driver to access a database server." http://php.net/manual/en/pdo.drivers.php – ficuscr May 02 '18 at 20:21
  • 3
    Possible duplicate of [PDOException “could not find driver”](https://stackoverflow.com/questions/2852748/pdoexception-could-not-find-driver) – ficuscr May 02 '18 at 20:25

3 Answers3

1

If you have installed php and the mysql-server properly, you have also to install the php mysql extension with

sudo apt-get install php-mysql

After that, you have the driver for mysqli and PDO.

0

You said:

I'm trying to connect to a remote mysql server

This is at odds with the line of code that includes $host = 'localhost';. The string localhost tells your database driver that the MySQL server is hosted on the same machine (aka IP address 127.0.0.1) as the server the PHP is running on.

You need to replace localhost with the domain name or IP address of the actual MySQL server. Also, make sure that the server is configured to allow external communications. I doubt it is configured in such a way by default.

Jacobm001
  • 4,431
  • 4
  • 30
  • 51
0

Make sure you have the PHP PDO extension installed: http://php.net/manual/en/book.pdo.php

Rick D.
  • 130
  • 2