必需(包含)mysql连接脚本在一个脚本中工作但在另一个脚本中不工作。 PHP

时间:2014-08-05 18:19:47

标签: php database mysqli database-connection

我有一个扩展的登录类,它扩展了php中的标准mysqli类 这是以下内容:

<?php
require_once("Config.php");

define(MYSQL_HOST, "host");
define(MYSQL_USER, "user");
define(MYSQL_PW, "password");
define(MYSQL_DB, "db");

/**
 * MysqlDB default wrapper, extend this if more specific needs are meant. Contains some helper functions for Select, Insert and Update.
 * @author Jerrpan
 * @version 1
 *
 */

class DB extends mysqli
{
    /**
     * Connects to db specified in Config.php file
     * @throws MysqlConnectError
     *      On failed connection
     */
    function __Construct() {
        parent::__construct(MYSQL_HOST, MYSQL_USER, MYSQL_PW, MYSQL_DB);
        if($this->connect_error) { 
            throw new MysqlConnectError($this->connect_error); }
    }

    /**
     * Finds out if db is up or not
     */

    private function isCon() {
        return $this->ping();
    }

    /**
     * Executes an Select statment
     * @param Assoc Array $data
     *      Array(
     *              "column"    => "column1, column2",
     *              "table"     => "table1, table2",
     *              "where"     => "column1 = value",
     *              "join"      => "INNERJOIN table ON column1 = column2",
     *              "orderby"   => "id DESC",
     *              "limit"     => 100
     *          )
     * @throws MysqlConnectError
     *      On lost mysql connection
     * @throws MysqlInvalidQuery
     *      On invalid query
     * @return void|stmt()
     *      On success an stmt object for further useage like storing and fetching.
     */

    public function Select($data) {
        if(!$this->isCon()) { throw new MysqlConnectError("Connection shut down"); }

        foreach($data as $key => $value) {
            if($value{0} == ",") { throw new MysqlInvalidQuery("Start or stop char cant be ,"); }
            if($value{strlen($value)} == ",") { throw new MysqlInvalidQuery("Start or stop char cant be ,"); }
        }

        if(isset($data["column"])) {
            $column = " SELECT {$data["column"]} "; 
        } else {
            throw new MysqlInvalidQuery("You have to specify columns");
        }

        if(isset($data["table"])) {
            $table = " FROM {$data["table"]} ";
        } else {
            throw new MysqlInvalidQuery("You have to specify tables");
        }

        if(isset($data["where"])) {
            $where = " WHERE {$data["where"]} ";
        }

        if(isset($data["join"])) {
            $innerjoin = " {$data["innerjoin"]} ";
        }

        if(isset($data["orderby"])) {
            $orderby = " {$data["orderby"]} ";
        }

        if(isset($data["limit"])) {
            $limit = " LIMIT {$data["limit"]} ";
        }

        $stmt = $this->query("$column $table $where $innerjoin $orderby $limit");
        if(!$stmt) { throw new MysqlInvalidQuery("Invalid query"); return;}
        return $stmt;
    }

    /**
     * Executes an insert statement
     * @param String $table
     *      The table to insert data into, can only be one
     * @param Assoc Array $data
     *      The data to insert as Colum => Value
     * @throws MysqlConnectError
     *      On lost db connection
     * @throws MysqlInvalidQuery
     *      On invalid query
     * @return boolean|void
     *      True on success
     */
    public function Insert($table, $data) { 
        $ins_table;
        $ins_value;

        if(!$this->isCon()) { throw new MysqlConnectError("Connection shut down"); }
        if(strstr($table, ",") != false) { throw new MysqlInvalidQuery("Several tables cant be inserted in one query"); }

        foreach($data as $key => $value) {
            $ins_table .= "$key,";
            $ins_value .= "$value,";
        }
            $ins_table = substr($ins_table, 0, -1);
            $ins_value = substr($ins_value, 0, -1);

        $this->query("INSERT INTO $table($ins_table) VALUES($ins_value)");
        if($this->affected_rows == 1) { return true; } else { throw new MysqlInvalidQuery("Invalid query"); }
    }

    /**
     * Executes an update statement
     * @param String $table
     *      The table to update, can not be more than one
     * @param Assoc Array $data
     *      The data to update the rows with in the context Column => Value
     * @param String $where
     *      The where statement without the WHERE
     * @param number $limit
     *      The limit, the default limit is 100
     * @throws MysqlConnectError
     *      On lost connection
     * @throws MysqlInvalidQuery
     *      On an invalid query
     * @return boolean|void
     *      On success true, on no affected rows false
     */
    public function Update($table, $data, $where, $limit = 100) {
        $ins;

        if(!$this->isCon()) { throw new MysqlConnectError("Connection shut down"); }

        foreach($data as $key => $value) {
            $ins .= " $key = $value , ";
        }
            $ins = substr($ins, 0, -2);

        $stmt = $this->query("UPDATE $table SET $ins WHERE $where LIMIT $limit" );
        if(!$stmt) { throw new MysqlInvalidQuery("Invalid query"); }
        if($this->affected_rows > 0) { return true; }

        return false;
    }
}

class MysqlConnectError     extends Exception {}
class MysqlInvalidQuery     extends Exception {}

然后通过将require_once('path/to/mysqlclass.php')添加到另一个脚本并创建DB()的新对象来使用它。

所以一切都很好。但是,当我尝试 require_once('../path/to/mysqlpath.php)(注意../)

我收到以下错误:

  

mysqli :: mysqli():php_network_getaddresses:getaddrinfo failed:名称或服务在其他地方未知......

我对此完全感到震惊,所以造成这个问题的原因是什么   我该如何解决?

1 个答案:

答案 0 :(得分:0)

require_once('../path/to/mysqlpath.php)之后,请检查 MYSQL_HOST的内容。

通常错误php_network_getaddresses: getaddrinfo failed表示, 您的服务器无法将域名example.com解析为IP地址。 因此,无论是dns服务器无法访问,还是网络服务器的DNS客户端配置错误,或者主机名称错误。

相关问题