2014-03-24 40 views
0

我正在创建一个照片库作为学习PHP的一部分。我的目录结构是这样的:mysql_select_db()期望参数2是资源

includes logs public

在包括目录,我有以下文件:

config.php database.php functions.php

里面的文件:

config.php

// Setup MySQL database connection 

    <?php 
    defined('DB_SERVER') ? null : define("DB_SERVER", "localhost"); // db server (usually localhost) 
    defined('DB_USER') ? null : define("DB_USER", "root"); // db user 
    defined('DB_PASS') ? null : define("DB_PASS", "usbw"); // db pass 
    defined('DB_NAME') ? null : define("DB_NAME", "photo_gallery"); // db name 
    ?> 

database.php

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

class MySQLDatabase { 

private $connection; 

function __construct() { 
    $this->open_connection(); 
} 

public function open_connection() { 
    $this->connection = mysqli_connect(DB_SERVER, DB_USER, DB_PASS); 
    if (!$this->connection) { 
     die("Database connection failed: " . mysql_error()); 
    } else { 
     $db_select = mysql_select_db(DB_NAME, $this->connection); 
     if (!$db_select) { 
      die("Database connection failed: " . mysql_error()); 
     } 
    } 
} 

public function close_connection() { 
if (isset($this->connection)) { 
    mysql_close($this->connection); 
    unset($this->connection); 
    } 
    } 

    public function query($sql) { 
    $result = mysql_query($sql, $this->connection); 
    $this->confirm_query($result); 
    return $result; 
    } 

    public function mysql_prep($value) { 
    $magic_quotes_active = get_magic_quotes_gpc(); 
    $new_enough_php = function_exists("mysql_real_escape_string"); // i.e. PHP >= 4.3.0 or higher 
    if ($new_enough_php) { // PHP >= 4.3.0 or higher 
// undo any magic quote effects so mysql_real_escape_string can do the work 
     if ($magic_quotes_active) { 
      $value = stripslashes($value); 
     } 
     $value = mysql_real_escape_string($value); 
    } else { //before PHP v4.3.0 
// if magic quotes aren't already on then add slashes manually 
     if (!$magic_quotes_active) { 
      $value = addslashes($value); 
     } 
     // if magic quotes are active, then slashes already exist 
    } 
    return $value; 
    } 

    private function confirm_query($result) { 
    if (!$result) { 
     die("Database connect failed " . mysql_error()); 
    } 
    } 
} 
$database = new MySQLDatabase(); 
$db =& $database; 
?> 

我创建内public -> index.php下面的代码来测试连接:

<?php 
require_once("../includes/database.php"); 

if (isset($database)) { echo "true"; } else { echo "false"; } 
echo "<br />"; 
?> 

当我在浏览器中加载这个页面我遇到以下错误:

Warning: mysql_select_db() expects parameter 2 to be resource, object given in D:\root\photo_gallery\includes\database.php on line 17 
Database connection failed: 

我试过删除$this->connection,只是有DB_NAME但是我被告知它无法连接到"@"localhost。谁能告诉我我做错了什么?

+0

**不再支持** mysql_ *'函数,它们是[**官方不推荐使用的**](https://wiki.php.net/rfc/mysql_deprecation),**不再维护**,将来会[**删除**](http://php.net/manual/en/function.mysql-connect.php#warning)。您应该使用[PDO](http://php.net/pdo)或[MySQLi](http://php.net/mysqli)更新您的代码,以确保您的项目未来的功能。 –

+0

您是否在使用mysqli连接使用'mysql_select_db()'? –

+0

我在这里使用mysqli:'$ this-> connection = mysqli_connect(DB_SERVER,DB_USER,DB_PASS);' –

回答

2

你使用MySQL

$this->connection = mysqli_connect(DB_SERVER, DB_USER, DB_PASS); 

混合的mysqli并采用

mysql_select_db(DB_NAME, $this->connection); 

使用mysqli_select_db http://in3.php.net/mysqli_select_db

另外你有mysql_所有功能*,他们也需要一定的照顾。

+0

啊,那真是谢谢你。我也写错了。我写道:'mysql_select_db(DB_NAME,$ this-> connection);'它应该已经'mysqli_select_db($ this-> connection,DB_NAME);' –

相关问题