Go Back   CodingForums.com > :: Server side development > MySQL

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 05-09-2011, 10:57 PM   PM User | #1
WhiskeyMike
New to the CF scene

 
Join Date: Mar 2011
Posts: 5
Thanks: 1
Thanked 0 Times in 0 Posts
WhiskeyMike is an unknown quantity at this point
[SOLVED] mysql_close not working

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:

Quote:
// 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:
Quote:
// 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:
Quote:
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?

Last edited by WhiskeyMike; 05-10-2011 at 04:57 AM.. Reason: Clarificiation
WhiskeyMike is offline   Reply With Quote
Old 05-10-2011, 04:22 AM   PM User | #2
Fumigator
UE Antagonizer


 
Fumigator's Avatar
 
Join Date: Dec 2005
Location: Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
Posts: 7,686
Thanks: 42
Thanked 637 Times in 625 Posts
Fumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of light
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).
__________________
Fumigator is offline   Reply With Quote
Users who have thanked Fumigator for this post:
WhiskeyMike (05-10-2011)
Old 05-10-2011, 04:54 AM   PM User | #3
WhiskeyMike
New to the CF scene

 
Join Date: Mar 2011
Posts: 5
Thanks: 1
Thanked 0 Times in 0 Posts
WhiskeyMike is an unknown quantity at this point
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.
WhiskeyMike is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:23 PM.


Advertisement
Log in to turn off these ads.