Mysqli_connect create error 500

June 7, 2017 329 views
MySQL Debian

Hey! I get HTTP error 500 everytime I try to use mysqli_connect in my php file.

This is my code and link to the file I am having trouble with:
http://138.68.86.134/getInfo.php

<?php
  session_start();

  if(!isset($_SESSION["access"])) {
    echo "null";
  } else if (isset($_SESSION["access"])) {
   // Here I am trying to use mysqli_connect
    $db = mysqli_connect("138.68.86.134", "root", "mypassword", "database")
    echo "Confirmed";
  } else {
    echo "Something went wrong...";
  }

 ?>

This is my Info.php file:
http://138.68.86.134/info.php

I've followed every step for Debain LAMP tutorial here on DigitalOcean still this doesn't work. I am very frustrated and I would highly appreciate any answer! Much love <3

2 Answers
Mohsen47 June 7, 2017
Accepted Answer

@southsweden

Try this quick solution and if it works I will explain it to you.

Replace 138.68.86.134 with 127.0.0.1 and try again.

  • It works thanks so much! Please explain how this works.

    • @southsweden

      Please mark my answer as accepted.

      I already explained that here:

      I think your problem is that the mysqld daemon is only listening on localhost and you are getting connection refused error and even if it is listening on all interfaces the root user by default is accessible only from localhost.

@southsweden

Before we get to troubleshooting, I would highly recommend and encourage you to use a created user instead of root. Should someone find a way in to your application, root access grants them the ability to remove everything (including other databases). You should never run as root :-).

...

That said, you're missing a ; at the end of the function call for mysqli_connect().

i.e. This line:

$db = mysqli_connect("138.68.86.134", "root", "mypassword", "database")

Should be:

$db = mysqli_connect("138.68.86.134", "root", "mypassword", "database");
  • @jtittle

    You are absolutely right.

    I think his problem is that the mysqld daemon is only listening on localhost and he gets connection refused error and even if it is listening on all interfaces the root user by default is accessible only from localhost

  • Yes thanks very clever. Never thought of that. How do I create a second user on phpmyadmin?

    • @southsweden

      I usually work from the terminal and does not use phpmyadmin but there must be a user's section above where you can create users and grant them privileges from the web UI.

      From the terminal you can use this query from mysql client:

      CREATE USER 'name'@'host' identified by 'password';

      GRANT ALL PRIVILEGES ON databasename.* TO 'user'@'host';

      `REVOKE DROP ON databasename.* FROM 'user'@'host'

      It is safer to revoke drop privileges from your database user in case your application is breached.

      • @southsweden @Mohsen47

        You could shorten that quite a bit ;).

        grant all on dbname.* to 'dbuser'@'localhost' identified by 'dbpass';
        

        dbname = Database Name

        dbuser = Database Username

        dbpass = Database Password

        • @jtittle

          You are right but the syntax you use is deprecated in mysql 5.7 and will be removed in future releases. 😊

Have another answer? Share your knowledge.