...

View Full Version : [SOLVED] mysql_close not working



WhiskeyMike
05-09-2011, 11:57 PM
Hello folks,

If this topic exists somewhere else on these forums, I apologize. I did do a search and did not find anything relevant. First, here's my script:



// First query to database: A, Table users
$sql = "INSERT INTO users (these, columns) ";
$sql .= "VALUES ('".$these."', '".$variables.") ";
// Do it!
if ($this->db == '' || $this->db != 1) {
$this->db = messagedbconnect(); // Connect function in included file
}
$result = mysql_query($sql) or trigger_error("SQL", E_USER_ERROR);
if ($this->db == 1 || $this->db != '') {
messagedbdconnect($this->db); // Disconnect function in included file
$this->db = '';
}

// Second query to database: B, Table members
$sql = "INSERT INTO members (different, columns) ";
$sql .= "VALUES ('".$different."', '".$variables.") ";
// Do it!
if ($this->db == 1 || $this->db != '') {
messagedbdconnect($this->db); // Just in case the other connection is, by some freak of nature, still open, we close it AGAIN
$this->db = ''; // Set the $db property to empty since we disconnected
}
if ($this->fdb == '' || $this->fdb != 1) {
$this->fdb = fdbconnect(); // DIFFERENT connection function in included file
}
$result = mysql_query($sql) or trigger_error("SQL", E_USER_ERROR);
if ($this->fdb == 1 || $this->fdb != '') {
fdbdconnect($this->fdb); // DIFFERENT disconnect function in included file
$this->fdb = ''; // Set the $fdb property to empty since we disconnected
}


And the included file:


// WEBSITE CONNECTION DATA //
define('SQL_SERVER','XXXX'); // Database Server
define('SQL_USER','XXXXXX'); // Your mysql User
define('SQL_PASS','XXXXXX'); // Your mysql Password
define('SQL_DB','database1'); // Your mysql database

function messagedbconnect() {
mysql_connect(SQL_SERVER,SQL_USER,SQL_PASS) or die("Error: ".mysql_error()); // Connection to the server
mysql_select_db(SQL_DB) or die("Error: ".mysql_error()); // Connecting to the database
return 1;
}
function messagedbdconnect($connection) {
mysql_close($connection);
return 1;
}

// FORUM CONNECTION DATA
define('FSQL_SERVER','XXXXXX'); // Database Server
define('FSQL_USER','XXXXXX'); // Your mysql User
define('FSQL_PASS','XXXXXX'); // Your mysql Password
define('FSQL_DB','database2'); // Your mysql database

function fdbconnect() {
mysql_connect(FSQL_SERVER,FSQL_USER,FSQL_PASS) or die("Error: ".mysql_error()); // Connection to the server
mysql_select_db(FSQL_DB) or die("Error: ".mysql_error()); // Connecting to the database
return 1;
}
function fdbdconnect($connection) {
mysql_close($connection);
return 1;
}


Now, even though I used mysql_close($this->db), it returns this error as if it's still thinking the "members" table should be part of database1:


MySQL error: 1146 : Table 'database1.members' doesn't exist (# 256)


This is frustrating me to no end... any ideas?

EDIT: I have the same mysql user for both databases, could that be the problem?

Fumigator
05-10-2011, 05:22 AM
The problem is you are sending $this->db to your disconnect function, and then you're using that variable as your database resource in the mysql_close() function, but the value of $this->db is simply 1, as you assigned that to it in your connect function (you are returning 1).

You probably want to return the actual database resource from your connect function instead of "1".

In reality, though, you can have multiple databases open at the same time, and then either use the mysql_select_db() function to choose the active database, or qualify the table names (etc) in your queries with the database name (ie SELECT * FROM database2.members).

WhiskeyMike
05-10-2011, 05:54 AM
Wow... it's always the smallest little detail. Thanks, works like a charm now! I am now including the database name with all queries ( database2.members ) as you suggested. Solved, thanks again.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum