0

I cannot connect to my database and I do not understand why.

I've run my php in a php checker with no errors.

I'm watching an online course and following the instructions to the letter, yet I'm getting the 'http 500 error' when I try to test and see if it works.

Heres my php:

<?php

//STEP 1. Declare params of user information
$email = htmlentities($_REQUEST["email"]);
$password = htmlentities($_REQUEST["password"]);

$test = "test";

if (empty($email) || empty($password)) {

    $returnArray["status"] = "400";
    $returnArray["message"] = "Missing required information";
    echo json_encode($returnArray);
    return;
    }

//secure password
$salt = openssl_random_pseudo_bytes(20);
$secured_password = sha1($password . $salt);

//build connection
//secure way to build connection
$file = parse_ini_file("../caps.ini");

//store in php var info from ini var
$host = trim($file["dbhost"]);
$user = trim($file["dbuser"]);
$pass = trim($file["dbpass"]);
$name = trim($file["dbname"]);


// include access.php to call func from access.php file 
require("secure/access.php");
$access = new access($host, $user, $pass, $name);
$access->connect();

?>

And here is the caps.ini file I made in a text editor, I've omitted the information here:

; Connection information
[section]
dbhost = omitted
dbuser = omitted
dbpass = omitted
dbname = omitted

And finally this is my php file where I reference my connection function:

<?php

//Declare class to access this php file
class access {

    //connection global variables
    var $host = null;
    var $user = null;
    var $pass = null;
    var $name = null;
    var $conn = null;
    var $result = null;

    // constructing class
    function __construct($dbhost, $dbuser, $dbpass, $dbname) {

    $this->host = $dbhost;
    $this->user = $dbuser;
    $this->pass = $dbpass;
    $this->name = $dbname;

    }

    // Connection Function
    public function connect() {

        //establish connection and store it in $conn
        $this->conn = new msqli($this->host, $this->user, $this->pass, $this->name);

        //if error
        if (mysqli_connect_errno()) {
            echo 'Could not connect to database';
        } else {
            echo "Connected";
        }
        //support all languages
        $this->conn->set_charset("utf8");

    }

    //disconnection function
    public function disconnect() {

        if ($this->conn != null) {
        $this->conn->close();
        }

    }


}

Here is my folder structure as well:

enter image description here

8
  • 1
    In your php I would try echo'ing something like echo "here"; followed by die(); and put it at the top of the page and move it down till you find where the problem is occuring. Commented Apr 17, 2017 at 22:05
  • 1
    Can you connect to your Database Server with another client? also new msqli( doesn't look right, perhapps you meant new mysqli( which you would have found with error reporting enabled. Commented Apr 17, 2017 at 22:07
  • What is your first file's path on the server? The file with // Step 1 in it Commented Apr 17, 2017 at 22:08
  • You're generating a random salt on every page load and then never saving it. Nobody will ever be able to log in. You should use password_hash() and password_verify() instead. Edit: Oh wait, I see, you just didn't post that part of the code. Still, don't use SHA1, it's not sufficient for security purposes. Commented Apr 17, 2017 at 22:08
  • also try hard-coding your $host, $user, $password, $name just long enough to verify that the values are correct. If correct, something wrong with getting the values from your file; otherwise, you need to provide the correct info. Commented Apr 17, 2017 at 22:09

1 Answer 1

1

My assumption is this typo around msqli vs mysqli

$this->conn = new msqli($this->host, $this->user, $this->pass, $this->name);

should be

$this->conn = new mysqli($this->host, $this->user, $this->pass, $this->name);
Sign up to request clarification or add additional context in comments.

1 Comment

That was it. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.