db in the same project

C++, C#, Java, PHP, ect...
Post Reply
Zester
Posts: 53
Joined: Sat Mar 03, 2012 11:33 pm

db in the same project

Post 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?
User avatar
Chris
Posts: 1580
Joined: Wed Sep 30, 2009 7:22 pm

Re: db in the same project

Post 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'");
 
Fighting for peace is declaring war on war. If you want peace be peaceful.
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: db in the same project

Post 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.
The indelible lord of tl;dr
Post Reply

Return to “Coding”