我的数据库无法连接

时间:2017-08-21 20:23:48

标签: javascript php mysql ajax

我不确定为什么我的数据库没有连接。我基本上试图用javascript生成一个网站,而我正在使用AJAX来调用php文件中的方法。这个方法是一个查询,所以我可以将这个查询的结果放到我的一些js组件中。有人可以帮忙吗?我已经被困了多年了!任何帮助表示赞赏。

我的javascript文件的一部分:

function callPHP() {
    $.ajax ({

        type: "GET",
        datatype: "application/json",
        url: "BaseClass.php",
        data: { action : 'getResults()' },
        //error: function(err){console.log(err)},
        success: function(output) {

            console.log(output);

        }
        //error, function(err){console.log(err)}
    });


}

callPHP();  

我的BaseClass(评论中的内容不起作用):

<?php

    error_reporting(E_ALL); ini_set('display_errors', 1);

    require("Conn.php");
    require("MySQLDao.php");

    $param=$_REQUEST['action'];

    /*
    $handle = fopen("php://input", "rb");
    $raw_post_data = '';
    while (!feof($handle)) {
        $raw_post_data .= fread($handle, 8192);
    }
    fclose($handle);
    */

    if (empty($param))
    {
        $returnValue["status"] = false;
        $returnValue["title"] = "Error";
        $returnValue["message"] = "No Data Recieved paige" .$param ."...";
        echo json_encode($returnValue);
        return;
    }
    else
    {
        $dao = new MySQLDao();
        if ($dao->openConnection() == false)
        {
            $returnValue["status"] = false;
            $returnValue["title"] = "Error";
            $returnValue["message"] = "Connection Could Not Be Established Between Server And Database";
            echo json_encode($returnValue);
        }
        else
        {
            //Decodes data, dont change
            $body = json_decode($raw_post_data, true);
            $recieved = $body["data"];

            //Gets the result of a query
            //$result = $dao->MySQLDaoMethodName(parameters);

            //Return the result of the query
            echo json_encode($result);
        }
        $dao->closeConnection();
        return;
    }
?>

我的conn.php:

<?php

    error_reporting(E_ALL); ini_set('display_errors', 1);

    class Conn
    {
        public static $dbhost = "***";
        public static $dbname = "***";
        public static $dbuser = "***";
        public static $dbpass = "***";
    }
?>

MySQLDao.php(此文件包含我希望从我的js文件中调用的方法):

<?php

    error_reporting(E_ALL); ini_set('display_errors', 1);

    //Class for holding queries
    class MySQLDao
    {
        var $dbhost = null;
        var $dbuser = null;
        var $dbpass = null;
        var $mysqli = null;
        var $dbname = null;
        var $result = null;


        //constructor
        function __construct()
        {
            $this->dbhost = Conn::$dbhost;
            $this->dbuser = Conn::$dbuser;
            $this->dbpass = Conn::$dbpass;
            $this->dbname = Conn::$dbname;
        }

        //Attempt a connection to the database
        public function openConnection()
        {   

            //Try and connect to the database
            $this->mysqli = new mysqli($this->dbhost, $this->dbuser, $this->dbpass, $this->dbname);
            //If the connection threw an error, report it
            if (mysqli_connect_errno())
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        //Get method for retrieving the database conection
        public function getConnection()
        {
            return $this->mysqli;
        }

        //Close the connection to the database
        public function closeConnection()
        {
            //If there is a connection to the database then close it
            if ($this->mysqli != null)
                $this->mysqli->close();
        }

        //-----------------------------------QUERY METHODS-------------------------------------

        public function getResults($data)
        {

            $sql = "SELECT room.room_description FROM room WHERE room.room_id = 1";

            $result = $this->mysqli->query($sql);


            //if (mysql_num_rows($result) == 1) {
            //  $obj = mysql_fetch_object($result, 'obResults');

            //}

            echo json_encode($result);

            echo($result);

        }

    }
?>

0 个答案:

没有答案