Yulek
Yulek

Reputation: 341

Cannot connect PHP to MySQL

I'm trying to get PHP to connect to MySQL and it's not working at all. I can log in and use MySQL from command line, but when I try to connect to it through PHP, all I get is a blank page. No error message or anything. Just to make sure, I searched around for solutions online but nothing seems to be working.

I am using PHP 5.5.9 and MySQL 5.5.43

Here is my code:

<?php
$servername = "127.0.0.1:3306";
$username = "user";
$password = "pass";

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

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

Upvotes: 0

Views: 656

Answers (2)

Yulek
Yulek

Reputation: 341

Finally fixed it. Turns out my php configuration files were a mess and I didn't have php5-mysql installed. I ended up reinstalling LAMP, which fixed the issue. For anyone having a similar problem, I found this answer very helpful: Fatal error: Class 'MySQLi' not found.

Upvotes: 0

user3383479
user3383479

Reputation:

You can try this:

<?php
$servername = "localhost"; //the port 3306 is the default
$username = "julek";
$password = "emelianenko";
//you should have the name of your database I presume which you can create a variable $database="name of your database";

// Create connection
 $conn= new  MySQLi($servername,$username,$password,$database)
 if ($conn->connect_error) {
   echo "Not connected, error: " . $conn->connect_error;
 }
 else {
   echo "Connected.";
 }

Upvotes: 1

Related Questions