Jump to content


Photo

using more than 1 database


  • Please log in to reply
8 replies to this topic

#1 leeming

leeming
  • Members
  • PipPipPip
  • Advanced Member
  • 93 posts

Posted 16 July 2006 - 03:00 AM

i am trying to write a function, so that i can switch been which database i am using, but i am having some problem with it.  ???

function CDB($id)
{
    /*******************************/
    /* Conects to databases        */
    /*******************************/
    
    GLOBAL $db;
    GLOBAL $admin_email;
    


    if($id == 0)  //database 1
    {
        mysql_select_db("*dbname*", $db)or die("<b>CRITICAL ERROR</b>: error code: '#F-con0'.<BR>Please report this to an admin at $admin_email.".mysql_error());
    }
    elseif($id == 1) // database 2
    {
        mysql_select_db("*dbname*", $db)or die("<b>CRITICAL ERROR</b>: error code: '#F-con1'.<BR>Please report this to an admin at $admin_email.".mysql_error());
    }
    else // no such database
    {
      die("<b>CRITICAL ERROR</b>: error code: '#F-Unk0'.<BR>Please report this to an admin at $admin_email.");
    }
}

i am calling it like...

mysql_query($sql, CDB(1))

but i know that isnt right.. and i cant seem to figgure this one out  ???
Im not perfect ;) lol

#2 hvle

hvle
  • Members
  • PipPipPip
  • Advanced Member
  • 667 posts
  • Locationmelbourne, Australia

Posted 16 July 2006 - 03:10 AM

what you are calling is wrong.  mysql_query take 1st arg as a query, and second arg as database link.  Therefore, function CDB() should return a link in order for mysql_query to work.

so my suggestion for your problem, is change CDB function so that it connect to db, select appropriate schema, and return that link.

something like this:

$dblink = mysql_connect(argvs here);

if (condition)
mysql_select_db(this db);
else mysql_select_db( that db);

return $dblink;

It must return a link.

Life's too short for arguing.

#3 leeming

leeming
  • Members
  • PipPipPip
  • Advanced Member
  • 93 posts

Posted 16 July 2006 - 03:23 AM

not fully following.. ive tried a few things... keep editing the code.. managed to get it 2 work, but had to call the function before a query..

ne way i return the selectDB i get..

Warning: mysql_select_db(): supplied argument is not a valid MySQL-Link resource in /home/.......


where the code is

return mysql_select_db("sector_wars", $db)

Im not perfect ;) lol

#4 hvle

hvle
  • Members
  • PipPipPip
  • Advanced Member
  • 667 posts
  • Locationmelbourne, Australia

Posted 16 July 2006 - 03:33 AM

you got that error because the $db is not a valid database link.  (did you connect to database?)

i see you do
GLOBAL $db;
GLOBAL $admin_email;

you must make sure $db is a valid link before global.
if you connect to database inside the function, you do not global the connection $db.

Life's too short for arguing.

#5 leeming

leeming
  • Members
  • PipPipPip
  • Advanced Member
  • 93 posts

Posted 16 July 2006 - 03:40 AM

$db is the conection info yes, removing it as global doesnt change the error i have...



i have thought away of doing it, but just wonder'd if there was a more simple way of doing it... (and easier for other people to read my code)

basic structure of the new function..
function newFunction ($sql, $db)
{
   *do selection for databases, using db as a name or id*
      *then connect to data base and connection*

     $outPut = mysql_query($sql, $conection);

    close conection

  return $outPut;

}

called as...

$sql = "select * from table";
$result = callfunction($sql, main database);


like i said.. seems a bit messy, and hard for other people to read, as they would be expecting mysql_query() ???
Im not perfect ;) lol

#6 hvle

hvle
  • Members
  • PipPipPip
  • Advanced Member
  • 667 posts
  • Locationmelbourne, Australia

Posted 16 July 2006 - 03:47 AM

This is very simple problem.  Why don't you tell us what you wanted to do.  Document out the function's responsibilty, and we can suggest a coding structure for it.

*do selection for databases, using db as a name or id*
*then connect to data base and connection*

This is a no-go.  You must successfully connected to database before you can select a database (schema).


Life's too short for arguing.

#7 leeming

leeming
  • Members
  • PipPipPip
  • Advanced Member
  • 93 posts

Posted 16 July 2006 - 03:50 AM

This is very simple problem.  Why don't you tell us what you wanted to do.  Document out the function's responsibilty, and we can suggest a coding structure for it.

*do selection for databases, using db as a name or id*
*then connect to data base and connection*

This is a no-go.  You must successfully connected to database before you can select a database (schema).


im just very stessed out 2night, thus not been able to think stright (been trying to fix this for hours)

basicly all im trying to do is, make it so i can have 2 database (both same connection details) just different names...

so lets say there is 1 query that needs to use database 1, instead of having

conection -> select db -> query -> close conection

every time i want to do a query... i could just instead choose the database while doing the mysql_query();
Im not perfect ;) lol

#8 hvle

hvle
  • Members
  • PipPipPip
  • Advanced Member
  • 667 posts
  • Locationmelbourne, Australia

Posted 16 July 2006 - 03:57 AM

try this block of function, fill out the neccessary information:
function my_db_selection($num)
{
    // connect to your db here
    $dblink = mysql_connect(argv here);
    // do a error handling here if $dblink is null

    // select schema:
    if ($num == 0)
        mysql_select_db('databasename0', $dblink); // fill in the name
    else if ($num == 1)
        mysql_select_db('databasename1', $dblink);
    else {
        echo 'invalid database selection';
        exit();
    }
    
    return $dblink;
}

then you can call a query like this:
mysql_query($sql, my_db_selection(0));
Life's too short for arguing.

#9 leeming

leeming
  • Members
  • PipPipPip
  • Advanced Member
  • 93 posts

Posted 16 July 2006 - 04:03 AM

try this block of function, fill out the neccessary information:

function my_db_selection($num)
{
    // connect to your db here
    $dblink = mysql_connect(argv here);
    // do a error handling here if $dblink is null

    // select schema:
    if ($num == 0)
        mysql_select_db('databasename0', $dblink); // fill in the name
    else if ($num == 1)
        mysql_select_db('databasename1', $dblink);
    else {
        echo 'invalid database selection';
        exit();
    }
    
    return $dblink;
}

then you can call a query like this:
mysql_query($sql, my_db_selection(0));


Thanks alot! i knew i was close with what i had.... yup i can finaly goto bed, now that its out o f the way lol
Im not perfect ;) lol




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users