Jump to content

Archived

This topic is now archived and is closed to further replies.

leeming

using more than 1 database

Recommended Posts

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.  ???

[code]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.");
    }
}[/code]

i am calling it like...

[code]mysql_query($sql, CDB(1))[/code]

but i know that isnt right.. and i cant seem to figgure this one out  ???

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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..

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

where the code is

[code]return mysql_select_db("sector_wars", $db)[/code]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
$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..
[code]
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;

}
[/code]

called as...

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


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

Share this post


Link to post
Share on other sites
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.

[quote]
*do selection for databases, using db as a name or id*
*then connect to data base and connection*
[/quote]
This is a no-go.  You must successfully connected to database before you can select a database (schema).

Share this post


Link to post
Share on other sites
[quote author=hvle link=topic=100745.msg398016#msg398016 date=1153021628]
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.

[quote]
*do selection for databases, using db as a name or id*
*then connect to data base and connection*
[/quote]
This is a no-go.  You must successfully connected to database before you can select a database (schema).


[/quote]
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();

Share this post


Link to post
Share on other sites
try this block of function, fill out the neccessary information:
[code]
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;
}
[/code]

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

Share this post


Link to post
Share on other sites
[quote author=hvle link=topic=100745.msg398024#msg398024 date=1153022236]
try this block of function, fill out the neccessary information:
[code]
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;
}
[/code]

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

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

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.