Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Mar 2011
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    [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:

    // 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?
    Last edited by WhiskeyMike; 05-10-2011 at 05:57 AM. Reason: Clarificiation

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    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).

  • Users who have thanked Fumigator for this post:

    WhiskeyMike (05-10-2011)

  • #3
    New to the CF scene
    Join Date
    Mar 2011
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •