php扩展类连接mysql问题

标签 php mysql

我有三个文件:

  • config.php
  • dbsng.php
  • bookclass.php

bookclass.php:

include 'config.php';
include 'dbsngt.php';

/**
* Books
*/
class Books extends Database
{

public $db;
protected $title;
protected $id;
protected $code;

function __construct()
{
$this->db = self::obtain(DB_SERVER, DB_USER, DB_PASS, DB_DATABASE);
if ($this->db->connect()){echo "YES connected";} else {echo "No connection";}

}

}

dbsngt.php:

class Database{

// debug flag for showing error messages
public  $debug = true;

// Store the single instance of Database
private static $instance;

private $server   = ""; //database server
private $user     = ""; //database login name
private $pass     = ""; //database login password
private $database = ""; //database name

private $error = "";

#######################
//number of rows affected by SQL query
public  $affected_rows = 0;

private $link_id = 0;
private $query_id = 0;


#-#############################################
# desc: constructor
private function __construct($server=null, $user=null, $pass=null, $database=null){
// error catching if not passed in
if($server==null || $user==null || $database==null){
    $this->oops("Database information must be passed in when the object is first created.");
}

$this->server=$server;
$this->user=$user;
$this->pass=$pass;
$this->database=$database;
}#-#constructor()


#-#############################################
# desc: singleton declaration
public static function obtain($server=null, $user=null, $pass=null, $database=null){
if (!self::$instance){ 
    self::$instance = new Database($server, $user, $pass, $database); 
} 

return self::$instance; 
}#-#obtain()


#-#############################################
# desc: connect and select database using vars above
# Param: $new_link can force connect() to open a new link, even if mysql_connect() was called before with the same parameters
public function connect($new_link=false){
$this->link_id=@mysql_connect($this->server,$this->user,$this->pass,$new_link);

if (!$this->link_id){//open failed
    $this->oops("Could not connect to server: <b>$this->server</b>.");
    }

if(!@mysql_select_db($this->database, $this->link_id)){//no database
    $this->oops("Could not open database: <b>$this->database</b>.");
    }

// unset the data so it can't be dumped
$this->server='';
$this->user='';
$this->pass='';
$this->database='';
}


}

当我执行以下代码时,它显示无连接 即使无法建立连接,我也继承了数据库类

include 'classes/bookclass.php';
$obj = new Books();

最佳答案

将数据库连接变量的私有(private)访问权限更改为公共(public)

public $server   = ""; //database server
public $user     = ""; //database login name
public $pass     = ""; //database login password
public $database = ""; //database name

关于php扩展类连接mysql问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24407044/

相关文章:

mysql - 如何从SQL表中获取不存在的记录?

mysql - 数据历史恢复

php - iOS 应用程序更新新的 MySQL 条目

php - Eloquent sortBy() 访问器不排序

php - 如何在 PHP 中实现高性能异步套接字服务器应用程序?

php - 如何从 Laravel 5 中的 MySQL DB 查询返回整数

Mysql Error 2008 客户端内存不足

php - PHP 对象/Sql 查询中的多数组结果

php - 配置发送邮件以发送我的地址

php - 使用 Bing.com 和 Bing Search API 的不同结果