0

PHP MySQL is not connecting... Here are the specs: Apache/2.2.15 (Unix) mysql Ver 14.14 Distrib 5.5.39, for Linux (x86_64) using readline 5.1 PHP 5.4.33 (cli) php-mysql for this version is loaded.

Briefing: This is a fresh Lamp install. I can connect to the database using SSH commands. The user I am trying to connect with has all permissions granted to the database I'm connecting to. Logging in using SSH with that user password from SSH works fine. I can access the database and select from tables.

When I try and connect from PHP I get cannot login to database.

Additional information: there is no domain pointing to the server. I have a virtual host setup in apache and I'm using my local host file to act as the DNS to access the server.

Any ideas?

Added:

 <?PHP
 $mysqli = new mysqli("localhost", "myuser", "mypassword", "mydatabase");
 if ($mysqli->connect_errno) {
     echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
 }
 ?>

1 Answers1

0

first try this

<?php
$con = mysqli_connect("localhost", "myuser", "mypassword", "mydatabase");

// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
?>

if above code does not work then try to connect using PDO

<?php
        $db_host="localhost";
        $db_pass='mypassword'; 
        $db_username="myuser";
        $db_name="mydatabase";
        $db = new PDO('mysql:host='.$db_host.';dbname='.$db_name,$db_username,$db_pass);
        echo "Connected successfully";
?>

hope this will work. :)

Domain
  • 11,562
  • 3
  • 23
  • 44