0

I have the following code:

<?php
//Step1
 $db = mysqli_connect('localhost','root','[mypassword]','users')
 or die('Error connecting to MySQL server.');
?>

<html>
 <head>
 </head>
 <body>
 <h1>PHP connect to MySQL</h1>
</body>
</html>

I am just trying to connect to my MySQL database. It is administered using phpMyAdmin. I am very unfamiliar with MySQL and I have never used it before. [mypassword] is the password I use to successfully connect to mySQL from the Mac terminal. "users" is a name of a table I have created in phpMyAdmin. I am using cPanel. I keep on getting the error:

Error connecting to mySQL server.

In phpMyAdmin it says Server: localhost:3306. I have tried for a very long time to fix this problem but with no results. What am I doing wrong?

I have also tried the following:

<?php
$servername = "localhost";
$username = "root";
$password = "[myPassword]";

try {
    $conn = new PDO("mysql:host=$servername;dbname=users", $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();
    }
?>

After visiting the webpage it says

Connection failed: SQLSTATE[28000] [1045] Access denied for user 'root'@'localhost' (using password: YES)

jarlh
  • 42,561
  • 8
  • 45
  • 63
Everett
  • 387
  • 1
  • 4
  • 17
  • 2
    Try using: `or die(mysqli_error($db))`, this will show you the reason for the failure, or even `mysqli_connect_error()` – Matt Clark Jul 03 '18 at 02:36
  • 1
    *Error connecting to mySQL server* is obviously your own error message from the shown code. [mysqli_error](http://php.net/manual/en/mysqli.error.php) may possibly produce more specific information about the problem. – faintsignal Jul 03 '18 at 02:37
  • I tried "or die(mysqli_error('localhost'));" and it didn't say anything on the webpage. – Everett Jul 03 '18 at 02:42
  • 1
    No, pass it the `$db` object. Exact line of code I posted. – Matt Clark Jul 03 '18 at 02:43
  • @Matt Clark I tried it with the exact line of code that you posted. On the webpage it says nothing. – Everett Jul 03 '18 at 02:50
  • Search for that error message from PDO on SO. You'll probably get the info you need among the first few hits. – faintsignal Jul 03 '18 at 02:51
  • 1
    Okay, try the second one then: [`mysqli_connect_error()`](http://php.net/manual/en/mysqli.connect-error.php). Follow the example given on this page. – Matt Clark Jul 03 '18 at 02:52
  • I tried your second one above. Now, on the webpage it says Access denied for user 'root'@'localhost' (using password: YES). – Everett Jul 03 '18 at 02:54

3 Answers3

1

Be sure that your mySql instance is up. You can download sequelpro and use that to connect to your mySQL. If that doesn't work then its a mysql setting/config that is wrong.

Cruze
  • 250
  • 3
  • 18
  • On my Mac in System Preferences it says that there is an active instance of mySQL. – Everett Jul 03 '18 at 02:43
  • have you tried sequelpro or mysqlworkbench to connect to your db? Basically were trying to isolate if this is PHP issue or installation issue – Cruze Jul 03 '18 at 02:58
  • I have downloaded and installed mySQLWorkBench. It says Local Instance 3306. When I click on it, it says "Cannot Connect to Database Server." It also says "Authentication plugin 'caching_sha2_password' cannot be loaded: dlopen(/usr/local/mysql/lib/plugin/caching_sha2_password.so." – Everett Jul 03 '18 at 03:04
  • when you installed mysql you should have selected old authentication or simple something like that. – Cruze Jul 03 '18 at 03:19
0

Use a try catch to handle errors on the connection proccess, but i strongly advise you to use PDO by alternative from mysqli PDO Book

Use something like this:

<?php
    try
    {
        if ($db = mysqli_connect($hostname_db, $username_db, $password_db))
        {
            //do something
        }
        else
        {
            throw new Exception('Unable to connect');
        }
    }
    catch(Exception $e)
    {
        echo $e->getMessage();
    }

And check if the code print some connection error.

Reference: how to use throw exception in mysql database connect

Soolie
  • 1,812
  • 9
  • 21
Renoir Reis
  • 359
  • 4
  • 17
  • I just tried this. On the webpage it said "Unable to connect." – Everett Jul 03 '18 at 02:40
  • 1
    This will not show the reason for the connection failure. Like OPs code, this will only show that an error has occurred. `mysqli_connect` is not throwing any exception, but is simply failing. It has its own methods to retrieve the failure reasons. – Matt Clark Jul 03 '18 at 02:40
  • 1
    @MattClark thats why i advised him to use PDO instead of mysqli_ it has better security and more flexibility with error handling. – Renoir Reis Jul 03 '18 at 02:43
  • I have tried the PDO method (I added to my original post above). It also gives an error. – Everett Jul 03 '18 at 02:48
  • Perhaps the problem is not on your code but on mysql server... could you please try to connect via terminal? – Renoir Reis Jul 03 '18 at 03:08
  • Connecting via terminal (as the root user with the same password I have been using) works succesfully. Connecting with my code above still does not work. – Everett Jul 04 '18 at 03:18
0

You can try this code

<?php
   $servername = "localhost";
   $username = "username";
   $password = "password";

   // Create connection
   $conn = mysqli_connect($servername, $username, $password);

   // Check connection
  if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
   }
   echo "Connected successfully";
  ?>

How to Connect to mysql using php

Salman Zafar
  • 3,844
  • 5
  • 20
  • 43