duanquan4451 2014-03-24 11:31
浏览 147
已采纳

mysql_select_db()期望参数2是资源

I am making a photo gallery as a part of learning PHP. My directory structure looks like this:

includes logs public

Within the includes directory I have the following files:

config.php database.php functions.php

Inside the files:

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;
?>

I created the following code within public -> index.php to test the connection:

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

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

When I load up this page in my browser I am met with the following error:

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

I have tried removing $this->connection and just having DB_NAME however I am then told it can't connect to "@"localhost. Can anyone tell me what I am doing wrong?

  • 写回答

1条回答 默认 最新

  • dst8922 2014-03-24 11:34
    关注

    You are mixing mysqli with mysql

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

    And using

    mysql_select_db(DB_NAME, $this->connection);
    

    Use mysqli_select_db http://in3.php.net/mysqli_select_db

    In addition you have all functions with mysql_* they also need some care.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 vmware exsi重置后的密码
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?
  • ¥15 电磁场的matlab仿真
  • ¥15 mars2d在vue3中的引入问题
  • ¥50 h5唤醒支付宝并跳转至向小荷包转账界面