简体   繁体   中英

Can't connect MySQL on server

If I try to connect to my MySQL database on my server(hosted on GoDaddy), I get this error:

Internal Server Error

The server encountered an internal error or misconfiguration and was unable to complete your request. Please contact the server administrator to inform of the time the error occurred and of anything you might have done that may have caused the error.

More information about this error may be available in the server error log.

I don't know how to get the server error log.

But if I try to connect to the database remotely(on my pc), It works fine.

I use this code:

<?php
class MY_SQL{
    private $username;
    private $password;
    private $conn;

    public function __construct($SERVERNAME){
        $this->username = "username";
        $this->password = "password";

        if($SERVERNAME == "data_"){
            $server = "server_ip";
        }
        else {
            $server = $SERVERNAME;
        }

        // Create connection
        $this->conn = new mysqli($server, $this->username, $this->password, "data_");

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

 //connect to database
 $database = new MY_SQL("data_");

What is going wrong?

Don't use mysqli becouse it's old use pdo.Also don't put everything in one file..... Here is a working code,i have organized it into folders.

index.php
scripts/
       dbcon/
            dbconfig.php
       functions
            start.php
            functions.php
       funcs.php

dbconfig.php

<?php
class Database{
  private $host = 'localhost';
  private $db_name = '';
  private $username = 'root';
  private $pass = 'root';
  public $conn;

  public function dbConnection(){
    $this -> conn = null;
    try{
      $this->conn = new PDO("mysql:host=".$this->host.";dbname=".$this->db_name,$this->username,$this->pass);
      $this->conn->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
    }catch(PDOException $e){
      echo "Connection error:".$e->getMessage();
    }
    return $this->conn;
  }
}
?>

start.php

<?php
ini_set('display_errors','On');
define('APP_ROOT',__DIR__);
define('ROOT',$_SERVER['DOCUMENT_ROOT']);

require_once ROOT."/scripts/dbcon/dbconfig.php";
session_start();

?>

functions.php

<?php
require_once 'start.php';

class ADMIN{
  private $conn;

  public function __construct(){
    $database = new Database();
    $db = $database->dbConnection();
    $this->conn = $db;
  }

  your functions

}

funcs.php

<?php
//Imports
require_once 'functions/functions.php';
//Classes
$functions = new ADMIN();

call your functions here from the functions.php

?>

Also don't use php as the main page ,create json in php and use polymer or node js o handle the json. :D

Problem solved! I had a deconstructor in my class which caused the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM