Page 1 of 1

db in the same project

Posted: Sun Mar 11, 2012 9:08 am
by Zester
How do I tell mysql what db to use?

this my dbconnect my db called ssg

Code: Select all

$db = mysql_connect("localhost","root","") or die("Could not connect to db");
if(!$db)
 die("no db");
if(!mysql_select_db("ssg",$db))
 die("No Database Selected");
and this is my dbconnect for my other db called lqlotl

Code: Select all

$db = mysql_connect("localhost","root","") or die("Could not connect to db");
if(!$db)
 die("no db");
if(!mysql_select_db("lqotl",$db))
 die("No Database Selected");
this most code in action

Code: Select all

$query = "select * from players where username='$username' ";
  $result = mysql_query($query) or die("Could not find user in players");
  $result2 = mysql_fetch_array($result);
  if (!$result2) {
	  	$regdate=$date=(date("Y/m/d"));	  
	  	$SQL = "INSERT into players (name, access, cssset, regdate,) VALUES ('$username','1','1','$regdate')";
		mysql_query($SQL) or die("could not register player");
		header("Location: playermade.php");	  
	   }
	   else {
	$lqlotlid = $result2['id'];
	$player = $result2['name'];
	$lqloflaccess =  $result2['access'];
	$cssset = $result2['cssset'];
	$regdate =  $result2['regdate'];
	$charslot1 = $result2['chars1'];
	$charslot2 = $result2['chars2'];
	$charslot3 = $result2['chars3'];	
	$charslot4 = $result2['chars4'];
	$charslot5 = $result2['chars5'];
	$charslot6 = $result2['chars6'];
			}
IN this case I want to pull from one db then the other, so my question this.
How do I tell one select statement to use the db called ssg, the other to use the dbcalled lqlotl?

cheers?

Re: db in the same project

Posted: Sun Mar 11, 2012 10:19 am
by Chris

Code: Select all

$ssg = mysql_connect('localhost','root');
mysql_select_db('ssg', $ssg);

$lqo = mysql_connect('localhost','root');
mysql_select_db('lqotl', $lqo); 

$query = mysql_query("SELECT * FROM players WHERE username='$username'", $ssg); 
$query2 = mysql_query("SELECT * FROM table WHERE field='lol'", $lqo); 
 
However if both databases are on the same server I'd advise doing this:

Code: Select all

mysql_connect('localhost','root');
$query = mysql_query("SELECT * FROM `ssg`.`players` WHERE `username` = '$username'");
 

Re: db in the same project

Posted: Sun Mar 11, 2012 8:56 pm
by Jackolantern
I second Chris' suggestion to use fully-qualified db names if you are using two. Keeping up with two different db connections is error-prone, and will begin to slow you down as you have to keep thinking about which one you need to use, tabbing to them to review columns, etc.