Forum Moderators: coopster

Message Too Old, No Replies

DB conn to a second DB

need to connect DB a and DB b

         

henry0

9:57 pm on Jul 12, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hello
that script is served from DB a
how wiill I code changes in that script in oder to connect to another DB on the same server
instead of
if (!@mysql_select_db("DB a"))
I need from the same location DB a to exit DB a and reach Db b by advance thank you

regards

<?

function db_connect()
{
$result = @mysql_pconnect("host", "username", "pw");
if (!$result)
return false;
if (!@mysql_select_db("DB a"))
return false;

return $result;
}

function get_writer_record($username)
{
$conn = db_connect();
$sql = "select * from manager where username = '$username'";
$result = mysql_query($sql, $conn);
return(mysql_fetch_array($result));
}

function get_new_content_record($new_content)
{
$conn = db_connect();
$sql = "select * from corp where id = '$new_content'";
$result = mysql_query($sql, $conn);
return(mysql_fetch_array($result));
}

?>

vincevincevince

11:04 pm on Jul 12, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



either write a new connect function


function db_connectb()
{
$result = @mysql_pconnect("host", "username", "pw");
if (!$result)
return false;
if (!@mysql_select_db("DB b"))
return false;
return $result;
}

or edit the existing one to take an argument


function db_connect($database)
{
$result = @mysql_pconnect("host", "username", "pw");
if (!$result)
return false;
if (!@mysql_select_db("DB $database"))
return false;
return $result;
}

//call with:
$conn=db_connect("a");
//or with:
$conn=db_connect("b");

or edit your sql that needs the 2nd database to refer to it:


$sql = "select * from `DB b.corp` where id = '$new_content'";

Vince

henry0

10:59 am on Jul 13, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Vincevincevince
thank you for the three options

Henry