php - mysql_select_db() expects parameter 2 to be resource -


i making photo gallery part of learning php. directory structure looks this:

includes logs public

within includes directory have following files:

config.php database.php functions.php

inside 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 magic quote effects mysql_real_escape_string can 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 on add slashes manually         if (!$magic_quotes_active) {             $value = addslashes($value);         }         // if magic quotes active, slashes exist     }     return $value;   }    private function confirm_query($result) {     if (!$result) {         die("database connect failed " . mysql_error());     }   } } $database = new mysqldatabase(); $db =& $database; ?> 

i created following code within public -> index.php test connection:

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

when load page in browser met following error:

warning: mysql_select_db() expects parameter 2 resource, object given in d:\root\photo_gallery\includes\database.php on line 17 database connection failed:  

i have tried removing $this->connection , having db_name told can't connect "@"localhost. can tell me doing wrong?

you mixing mysqli 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 have functions mysql_* need care.


Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -