0

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";
Yulek
  • 331
  • 3
  • 12
  • Can you show your db connection code? then only we can suggest what is wrong happening? – Alive to die - Anant May 09 '15 at 01:14
  • Can we see the code that generates that blank page? Also, did you look at the error log? – fvu May 09 '15 at 01:14
  • 2
    also be sure that php errors are turned on, that's often the cause of the white page. you can toggle it on in your .htaccess file and then you might get a usable error message – Rocky May 09 '15 at 01:18
  • This is the error I am getting: Fatal error: Call to undefined function mysqli_connect(). – Yulek May 09 '15 at 01:49
  • 0 down vote Check the below url, might help you. http://webcheatsheet.com/php/connect_mysql_database.php – MZaragoza May 09 '15 at 02:30

2 Answers2

1

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.";
 }
  • Tried it but still nothing. Still all I get is a blank page, so I don't even know what I need to fix. – Yulek May 09 '15 at 01:35
  • did you created the $database variable?? If you have a blank page instead of "connexion failed" it means it is ok... could you show ho you call it? –  May 09 '15 at 01:35
  • yeah, I edited a few things so now I get this error: Fatal error: Call to undefined function mysqli_connect() – Yulek May 09 '15 at 01:52
  • I'm on Ubuntu with LAMP installed – Yulek May 09 '15 at 02:00
0

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.

Community
  • 1
  • 1
Yulek
  • 331
  • 3
  • 12