Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 访问数据库_Php_Mysql_Sql_Directory_Terminal - Fatal编程技术网

Php 访问数据库

Php 访问数据库,php,mysql,sql,directory,terminal,Php,Mysql,Sql,Directory,Terminal,我正在用MacOSX Lion服务器运行一个MySQL数据库。我的服务器上运行了一个PHP脚本,希望访问数据库。PHP脚本如下所示: /库/服务器/Web/数据/站点/默认值 数据库如下: /usr/local/mysql 如果PHP脚本不在同一目录中(数据库在其上方),如何让它们访问数据库。谢谢你的帮助 以下是我遇到的错误: Connect failed: No such file or directory?There seems to have been a slight problem

我正在用MacOSX Lion服务器运行一个MySQL数据库。我的服务器上运行了一个PHP脚本,希望访问数据库。PHP脚本如下所示:

/库/服务器/Web/数据/站点/默认值

数据库如下:

/usr/local/mysql

如果PHP脚本不在同一目录中(数据库在其上方),如何让它们访问数据库。谢谢你的帮助

以下是我遇到的错误:

Connect failed: No such file or directory?There seems to have been a  slight problem with our database, please try again later.<br /><br />? <textarea rows="10" cols="80">MySQL Error:???42??Error: ?Error #: ?  Filename:
其余代码如下:

/**
 * Begin Document
 */

class DbConnect
{
/**
* Connection to MySQL.
*
* @var string
*/
var $link;

/**
* Holds the most recent connection.
*
* @var string
*/
var $recent_link = null;

/**
* Holds the contents of the most recent SQL query.
*
* @var string
*/
var $sql = '';

/**
* Holds the number of queries executed.
*
* @var integer
*/
var $query_count = 0;

/**
* The text of the most recent database error message.
*
* @var string
*/
var $error = '';

/**
* The error number of the most recent database error message.
*
* @var integer
*/
var $errno = '';

/**
* Do we currently have a lock in place?
*
* @var boolean
*/
var $is_locked = false;

/**
* Show errors? If set to true, the error message/sql is displayed.
*
* @var boolean
*/
var $show_errors = false;

/**
* Log errors? If set to true, the error message/sql is logged.
*
* @var boolean
*/
public $log_errors = false;

/**
* The Database.
*
* @var string
*/
public $DB_DATABASE;

/**
* The variable used to contain a singleton instance of the database connection.
*
* @var string
*/
static $instance;

/**
* The number of rows affected by the most recent query.
*
* @var string
*/
public $affected_rows;

public $insert_id;



/**
* Constructor. Initializes a database connection and selects our database.
*/
function __construct()
{
    $this->DB_HOST     = '67.85.14.141';
    $this->DB_USERNAME = 'username'; // !!! CHANGE ME
    $this->DB_PASSWORD = 'password'; // !!! CHANGE ME
    $this->DB_DATABASE = 'Carillons'; // !!! CHANGE ME
}

/**
* Singleton pattern to retrieve database connection.
*
* @return mixed MySQL database connection
*/
function _get($property)
{
    if(self::$instance == NULL)
    {
        self::$instance = $this->connect();
    }

    return self::$instance->$property;

}


/**
* Singleton pattern to retrieve database connection.
*
* @return mixed MySQL database connection
*/
function Connection()
{
    if(self::$instance == NULL)
    {
        self::$instance = $this->connect();
    }
    return self::$instance;
}


/**
* Connect to the Database.
*
*/
function connect()
{
    self::$instance = new mysqli($this->DB_HOST, $this->DB_USERNAME, $this->DB_PASSWORD, $this->DB_DATABASE);

    if (mysqli_connect_errno()) {
        $this->raise_error(printf("Connect failed: %s\n", mysqli_connect_error()));
    }

    return self::$instance;
}


/**
* Executes a sql query. If optional $only_first is set to true, it will
* return the first row of the result as an array.
*
* @param  string  Query to run
* @param  bool    Return only the first row, as an array?
* @return mixed
*/
function query($sql, $only_first = false)
{
    if(self::$instance == NULL)
    {
        self::$instance = $this->connect();
    }

    $this->recent_link =& self::$instance;
    $this->sql =& $sql;

    if(!$result = self::$instance->query($sql))
    {
        $this->raise_error(printf("Connect failed: %s\n", self::$instance->error));
    }

    $this->affected_rows = self::$instance->affected_rows;
    $this->insert_id = self::$instance->insert_id;
    $this->query_count++;

    if ($only_first)
    {
        $return = $result->fetch_array(MYSQLI_ASSOC);
        $this->free_result($result);
        return $return;
    }
    return $result;
}

/**
* Fetches a row from a query result and returns the values from that row as an array.
*
* @param  string  The query result we are dealing with.
* @return array
*/
function fetch_array($result)
{
    return @mysql_fetch_assoc($result);
}

/**
* Returns the number of rows in a result set.
*
* @param  string  The query result we are dealing with.
* @return integer
*/
function num_rows($result)
{
    return self::$instance->num_rows;
}

/**
* Retuns the number of rows affected by the most recent query
*
* @return integer
*/
function affected_rows()
{
    return self::$instance->affected_rows;
}


/**
* Returns the number of queries executed.
*
* @param  none
* @return integer
*/
function num_queries()
{
    return $this->query_count;
}

/**
* Lock database tables
*
* @param   array  Array of table => lock type
* @return  void
*/
function lock($tables)
{
    if (is_array($tables) AND count($tables))
    {
        $sql = '';

        foreach ($tables AS $name => $type)
        {
            $sql .= (!empty($sql) ? ', ' : '') . "$name $type";
        }

        $this->query("LOCK TABLES $sql");
        $this->is_locked = true;
    }
}

/**
* Unlock tables
*/
function unlock()
{
    if ($this->is_locked)
    {
        $this->query("UNLOCK TABLES");
    }
}

/**
* Returns the ID of the most recently inserted item in an auto_increment field
*
* @return  integer
*/
function insert_id()
{
    return self::$instance->insert_id;
}

/**
* Escapes a value to make it safe for using in queries.
*
* @param  string  Value to be escaped
* @param  bool    Do we need to escape this string for a LIKE statement?
* @return string
*/
function prepare($value, $do_like = false)
{
    if(self::$instance == NULL)
    {
        self::$instance = $this->connect();
    }

    if ($do_like)
    {
        $value = str_replace(array('%', '_'), array('\%', '\_'), $value);
    }

    return self::$instance->real_escape_string($value);
}

/**
* Frees memory associated with a query result.
*
* @param  string   The query result we are dealing with.
* @return boolean
*/
function free_result($result)
{
    return @mysql_free_result($result);
}

/**
* Turns database error reporting on
*/
function show_errors()
{
    $this->show_errors = true;
}

/**
* Turns database error reporting off
*/
function hide_errors()
{
    $this->show_errors = false;
}

/**
* Closes our connection to MySQL.
*
* @param  none
* @return boolean
*/
function close()
{
    $this->sql = '';
    return self::$instance->close();
}

/**
* Returns the MySQL error message.
*
* @param  none
* @return string
*/
function error()
{
    $this->error = (is_null($this->recent_link)) ? '' : self::$instance->error; 
    return $this->error;
}

/**
* Returns the MySQL error number.
*
* @param  none
* @return string
*/
function errno()
{
    $this->errno = (is_null($this->recent_link)) ? 0 : self::$instance->errno ;
    return $this->errno;
}

/**
* Gets the url/path of where we are when a MySQL error occurs.
*
* @access private
* @param  none
* @return string
*/
function _get_error_path()
{
    if ($_SERVER['REQUEST_URI'])
    {
        $errorpath = $_SERVER['REQUEST_URI'];
    }
    else
    {
        if ($_SERVER['PATH_INFO'])
        {
            $errorpath = $_SERVER['PATH_INFO'];
        }
        else
        {
            $errorpath = $_SERVER['PHP_SELF'];
        }

        if ($_SERVER['QUERY_STRING'])
        {
            $errorpath .= '?' . $_SERVER['QUERY_STRING'];
        }
    }

    if (($pos = strpos($errorpath, '?')) !== false)
    {
        $errorpath = urldecode(substr($errorpath, 0, $pos)) . substr($errorpath, $pos);
    }
    else
    {
        $errorpath = urldecode($errorpath);
    }
    return $_SERVER['HTTP_HOST'] . $errorpath;
}

/**
* If there is a database error, the script will be stopped and an error message displayed.
*
* @param  string  The error message. If empty, one will be built with $this->sql.
* @return string
*/
function raise_error($error_message = '')
{
    if ($this->recent_link)
    {
        $this->error = $this->error($this->recent_link);
        $this->errno = $this->errno($this->recent_link);
    }

    if ($error_message == '')
    {
        $this->sql = "Error in SQL query:\n\n" . rtrim($this->sql) . ';';
        $error_message =& $this->sql;
    }
    else
    {
        $error_message = $error_message . ($this->sql != '' ? "\n\nSQL:" . rtrim($this->sql) . ';' : '');
    }

    $message = "<textarea rows=\"10\" cols=\"80\">MySQL Error:\n\n\n$error_message\n\nError: {$this->error}\nError #: {$this->errno}\nFilename: " . $this->_get_error_path() . "\n</textarea>";

    if (!$this->show_errors)
    {
        $message = "<!--\n\n$message\n\n-->";
    }
    else die("There seems to have been a slight problem with our database, please try again later.<br /><br />\n$message");
}
}

?>
/**
*开始文档
*/
类DbConnect
{
/**
*连接到MySQL。
*
*@var字符串
*/
var$link;
/**
*保存最近的连接。
*
*@var字符串
*/
var$recent_link=null;
/**
*保存最新SQL查询的内容。
*
*@var字符串
*/
var$sql='';
/**
*保存执行的查询数。
*
*@var整数
*/
var$query\u count=0;
/**
*最新数据库错误消息的文本。
*
*@var字符串
*/
var$error='';
/**
*最新数据库错误消息的错误号。
*
*@var整数
*/
var$errno='';
/**
*我们现在有锁吗?
*
*@var布尔值
*/
var$已锁定=错误;
/**
*显示错误?如果设置为true,则显示错误消息/sql。
*
*@var布尔值
*/
var$show_errors=false;
/**
*记录错误?如果设置为true,则记录错误消息/sql。
*
*@var布尔值
*/
public$log_errors=false;
/**
*数据库。
*
*@var字符串
*/
公共$DB_数据库;
/**
*用于包含数据库连接的单例实例的变量。
*
*@var字符串
*/
静态$实例;
/**
*受最近查询影响的行数。
*
*@var字符串
*/
受影响的公共行数;
公共$insert_id;
/**
*初始化数据库连接并选择我们的数据库。
*/
函数_u构造()
{
$this->DB_HOST='67.85.14.141';
$this->DB_USERNAME='USERNAME';/!!!更改我
$this->DB_PASSWORD='PASSWORD';/!!!更改我
$this->DB_数据库='Carillons';/!!!更改我
}
/**
*检索数据库连接的单例模式。
*
*@return混合MySQL数据库连接
*/
函数_get($property)
{
if(self::$instance==NULL)
{
self::$instance=$this->connect();
}
返回self::$instance->$property;
}
/**
*检索数据库连接的单例模式。
*
*@return混合MySQL数据库连接
*/
函数连接()
{
if(self::$instance==NULL)
{
self::$instance=$this->connect();
}
返回self::$instance;
}
/**
*连接到数据库。
*
*/
函数连接()
{
self::$instance=newmysqli($this->DB\u主机,$this->DB\u用户名,$this->DB\u密码,$this->DB\u数据库);
if(mysqli\u connect\u errno()){
$this->raise_error(printf(“连接失败:%s\n”,mysqli_Connect_error());
}
返回self::$instance;
}
/**
*执行sql查询。如果可选的$only_first设置为true,则它将
*以数组形式返回结果的第一行。
*
*@param要运行的字符串查询
*@param bool仅返回第一行,作为数组?
*@返回混合
*/
函数查询($sql,$only\u first=false)
{
if(self::$instance==NULL)
{
self::$instance=$this->connect();
}
$this->recent_link=&self::$instance;
$this->sql=&$sql;
如果(!$result=self::$instance->query($sql))
{
$this->raise_错误(printf(“连接失败:%s\n”,self::$instance->error));
}
$this->infected_rows=self::$instance->infected_rows;
$this->insert\u id=self::$instance->insert\u id;
$this->query_count++;
如果($only_first)
{
$return=$result->fetch_数组(MYSQLI_ASSOC);
$this->free_result($result);
return$return;
}
返回$result;
}
/**
*从查询结果中获取一行,并将该行中的值作为数组返回。
*
*@param string我们正在处理的查询结果。
*@return数组
*/
函数fetch_数组($result)
{
return@mysql\u fetch\u assoc($result);
}
/**
*返回结果集中的行数。
*
*@param string我们正在处理的查询结果。
*@返回整数
*/
函数num_行($result)
{
返回self::$instance->num\u行;
}
/**
*重新运行受最近查询影响的行数
*
*@返回整数
*/
受影响的函数\u行()
{
返回self::$instance->受影响的_行;
}
/**
*返回执行的查询数。
*
*@param-none
*@返回整数
*/
函数num_querys()
{
返回$this->query\u count;
}
/**
*锁定数据库表
*
*@param表数组=>锁类型
*@返回无效
*/
函数锁($tables)
{
if(is_数组($tables)和count($tables))
{
$sql='';
foreach($name=>$type形式的表)
{
$sql.=(!empty($sql)?,':''。“$name$type”;
}
$this->query(“锁表$sql”);
$this->is_locked=true;
}
}
/**
*解锁表格
*/
函数解锁()
{
如果($此->已锁定)
{
$this->query(“解锁表”);
}
}
/**
*返回自动增量字段中最近插入的项的ID
*
*@返回整数
*/
函数insert_id()
{
返回self::$instance->insert_id;
}
/**
*转义一个值以使其在查询中使用安全。
*
*要转义的@param字符串值
*@param bool我们需要为LIKE语句转义这个字符串吗?
*@返回字符串
*/
函数prepare($value,$do_like=false)
{
if(self::$instance==NULL)
{
self::$instance=$this->connect();
}
如果($do_like)
{
$value=str\u replace(数组('%','\''),数组('\%','\\''),$value);
}
返回self::$instance->real\u escape\u string($value);
}
/**
*释放与查询结果关联的内存。
*
*@param string我们正在处理的查询结果。
*@返回布尔值
*/
函数自由_结果($result)
{
返回@mysql\u free\u result($result);
}
/**
*打开数据库错误报告
*/
函数show_errors()
{
$this->show_errors=true;
}
/**
*关闭数据库错误报告
*/
函数隐藏错误
/**
 * Begin Document
 */

class DbConnect
{
/**
* Connection to MySQL.
*
* @var string
*/
var $link;

/**
* Holds the most recent connection.
*
* @var string
*/
var $recent_link = null;

/**
* Holds the contents of the most recent SQL query.
*
* @var string
*/
var $sql = '';

/**
* Holds the number of queries executed.
*
* @var integer
*/
var $query_count = 0;

/**
* The text of the most recent database error message.
*
* @var string
*/
var $error = '';

/**
* The error number of the most recent database error message.
*
* @var integer
*/
var $errno = '';

/**
* Do we currently have a lock in place?
*
* @var boolean
*/
var $is_locked = false;

/**
* Show errors? If set to true, the error message/sql is displayed.
*
* @var boolean
*/
var $show_errors = false;

/**
* Log errors? If set to true, the error message/sql is logged.
*
* @var boolean
*/
public $log_errors = false;

/**
* The Database.
*
* @var string
*/
public $DB_DATABASE;

/**
* The variable used to contain a singleton instance of the database connection.
*
* @var string
*/
static $instance;

/**
* The number of rows affected by the most recent query.
*
* @var string
*/
public $affected_rows;

public $insert_id;



/**
* Constructor. Initializes a database connection and selects our database.
*/
function __construct()
{
    $this->DB_HOST     = '67.85.14.141';
    $this->DB_USERNAME = 'username'; // !!! CHANGE ME
    $this->DB_PASSWORD = 'password'; // !!! CHANGE ME
    $this->DB_DATABASE = 'Carillons'; // !!! CHANGE ME
}

/**
* Singleton pattern to retrieve database connection.
*
* @return mixed MySQL database connection
*/
function _get($property)
{
    if(self::$instance == NULL)
    {
        self::$instance = $this->connect();
    }

    return self::$instance->$property;

}


/**
* Singleton pattern to retrieve database connection.
*
* @return mixed MySQL database connection
*/
function Connection()
{
    if(self::$instance == NULL)
    {
        self::$instance = $this->connect();
    }
    return self::$instance;
}


/**
* Connect to the Database.
*
*/
function connect()
{
    self::$instance = new mysqli($this->DB_HOST, $this->DB_USERNAME, $this->DB_PASSWORD, $this->DB_DATABASE);

    if (mysqli_connect_errno()) {
        $this->raise_error(printf("Connect failed: %s\n", mysqli_connect_error()));
    }

    return self::$instance;
}


/**
* Executes a sql query. If optional $only_first is set to true, it will
* return the first row of the result as an array.
*
* @param  string  Query to run
* @param  bool    Return only the first row, as an array?
* @return mixed
*/
function query($sql, $only_first = false)
{
    if(self::$instance == NULL)
    {
        self::$instance = $this->connect();
    }

    $this->recent_link =& self::$instance;
    $this->sql =& $sql;

    if(!$result = self::$instance->query($sql))
    {
        $this->raise_error(printf("Connect failed: %s\n", self::$instance->error));
    }

    $this->affected_rows = self::$instance->affected_rows;
    $this->insert_id = self::$instance->insert_id;
    $this->query_count++;

    if ($only_first)
    {
        $return = $result->fetch_array(MYSQLI_ASSOC);
        $this->free_result($result);
        return $return;
    }
    return $result;
}

/**
* Fetches a row from a query result and returns the values from that row as an array.
*
* @param  string  The query result we are dealing with.
* @return array
*/
function fetch_array($result)
{
    return @mysql_fetch_assoc($result);
}

/**
* Returns the number of rows in a result set.
*
* @param  string  The query result we are dealing with.
* @return integer
*/
function num_rows($result)
{
    return self::$instance->num_rows;
}

/**
* Retuns the number of rows affected by the most recent query
*
* @return integer
*/
function affected_rows()
{
    return self::$instance->affected_rows;
}


/**
* Returns the number of queries executed.
*
* @param  none
* @return integer
*/
function num_queries()
{
    return $this->query_count;
}

/**
* Lock database tables
*
* @param   array  Array of table => lock type
* @return  void
*/
function lock($tables)
{
    if (is_array($tables) AND count($tables))
    {
        $sql = '';

        foreach ($tables AS $name => $type)
        {
            $sql .= (!empty($sql) ? ', ' : '') . "$name $type";
        }

        $this->query("LOCK TABLES $sql");
        $this->is_locked = true;
    }
}

/**
* Unlock tables
*/
function unlock()
{
    if ($this->is_locked)
    {
        $this->query("UNLOCK TABLES");
    }
}

/**
* Returns the ID of the most recently inserted item in an auto_increment field
*
* @return  integer
*/
function insert_id()
{
    return self::$instance->insert_id;
}

/**
* Escapes a value to make it safe for using in queries.
*
* @param  string  Value to be escaped
* @param  bool    Do we need to escape this string for a LIKE statement?
* @return string
*/
function prepare($value, $do_like = false)
{
    if(self::$instance == NULL)
    {
        self::$instance = $this->connect();
    }

    if ($do_like)
    {
        $value = str_replace(array('%', '_'), array('\%', '\_'), $value);
    }

    return self::$instance->real_escape_string($value);
}

/**
* Frees memory associated with a query result.
*
* @param  string   The query result we are dealing with.
* @return boolean
*/
function free_result($result)
{
    return @mysql_free_result($result);
}

/**
* Turns database error reporting on
*/
function show_errors()
{
    $this->show_errors = true;
}

/**
* Turns database error reporting off
*/
function hide_errors()
{
    $this->show_errors = false;
}

/**
* Closes our connection to MySQL.
*
* @param  none
* @return boolean
*/
function close()
{
    $this->sql = '';
    return self::$instance->close();
}

/**
* Returns the MySQL error message.
*
* @param  none
* @return string
*/
function error()
{
    $this->error = (is_null($this->recent_link)) ? '' : self::$instance->error; 
    return $this->error;
}

/**
* Returns the MySQL error number.
*
* @param  none
* @return string
*/
function errno()
{
    $this->errno = (is_null($this->recent_link)) ? 0 : self::$instance->errno ;
    return $this->errno;
}

/**
* Gets the url/path of where we are when a MySQL error occurs.
*
* @access private
* @param  none
* @return string
*/
function _get_error_path()
{
    if ($_SERVER['REQUEST_URI'])
    {
        $errorpath = $_SERVER['REQUEST_URI'];
    }
    else
    {
        if ($_SERVER['PATH_INFO'])
        {
            $errorpath = $_SERVER['PATH_INFO'];
        }
        else
        {
            $errorpath = $_SERVER['PHP_SELF'];
        }

        if ($_SERVER['QUERY_STRING'])
        {
            $errorpath .= '?' . $_SERVER['QUERY_STRING'];
        }
    }

    if (($pos = strpos($errorpath, '?')) !== false)
    {
        $errorpath = urldecode(substr($errorpath, 0, $pos)) . substr($errorpath, $pos);
    }
    else
    {
        $errorpath = urldecode($errorpath);
    }
    return $_SERVER['HTTP_HOST'] . $errorpath;
}

/**
* If there is a database error, the script will be stopped and an error message displayed.
*
* @param  string  The error message. If empty, one will be built with $this->sql.
* @return string
*/
function raise_error($error_message = '')
{
    if ($this->recent_link)
    {
        $this->error = $this->error($this->recent_link);
        $this->errno = $this->errno($this->recent_link);
    }

    if ($error_message == '')
    {
        $this->sql = "Error in SQL query:\n\n" . rtrim($this->sql) . ';';
        $error_message =& $this->sql;
    }
    else
    {
        $error_message = $error_message . ($this->sql != '' ? "\n\nSQL:" . rtrim($this->sql) . ';' : '');
    }

    $message = "<textarea rows=\"10\" cols=\"80\">MySQL Error:\n\n\n$error_message\n\nError: {$this->error}\nError #: {$this->errno}\nFilename: " . $this->_get_error_path() . "\n</textarea>";

    if (!$this->show_errors)
    {
        $message = "<!--\n\n$message\n\n-->";
    }
    else die("There seems to have been a slight problem with our database, please try again later.<br /><br />\n$message");
}
}

?>