WebProWorld Part of WebProNews.com
Page One Link To Us Edit Profile Private Messages Archives FAQ RSS Feeds  
 

Go Back   WebProWorld > Webmaster, IT and Security Discussion > Database Discussion Forum
Subscribe to the Newsletter FREE!


Register FAQ Members List Calendar Arcade Chatbox Mark Forums Read

Database Discussion Forum This is the place to find help resolving those nagging questions you have about implementing and using all kinds of databases. Need help writing a query? Need an opinion on Oracle? Post here!

Closed Thread
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-15-2007, 06:56 PM
jazzmatazz2005 jazzmatazz2005 is offline
WebProWorld Member
 

Join Date: Aug 2003
Location: Jersey City, NJ
Posts: 71
jazzmatazz2005 RepRank 0
Default 2 databases

i been trying to figure this out for a while.

how do i change this login script to check 2 databases.

for this its safe to assume that the 2nd database has the same table names

Code:
<?php
	session_start();
	mysql_connect( 'localhost', 'root', '' );
	mysql_select_db( 'login' );
	
	if( $_POST['login'] ){
		$query = "SELECT * FROM ims WHERE email = '".$_POST['user']."' AND password = '".$_POST['pass']."'";
		$result = mysql_query( $query );
		if(  mysql_num_rows( $result ) ){
			$row = mysql_fetch_array( $result );
			$_SESSION['fname'] = $row['fname'];
			header( 'Location: welcome.php' );
		}
		else
			header( 'Location: errlogin.php' );
	}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Log in</title>
</head>

<body>
<form method="post">
username <input name="user" type="text" />

password <input name="pass" type="text" />

<input name="login" type="submit" value="login" />
</form>
</body>
</html>
__________________
It's better to do business with me than against me!
  #2 (permalink)  
Old 05-15-2007, 09:57 PM
studiokraft studiokraft is offline
WebProWorld New Member
 

Join Date: Jun 2006
Location: NorthEast
Posts: 23
studiokraft RepRank 0
Default Re: 2 databases

Quote:
Originally Posted by jazzmatazz2005
how do i change this login script to check 2 databases.
Assuming that the databases exist on the same server, you should be able use mysql_db_query() instead of the combination of mysql_select_db() and mysql_query().

http://us.php.net/function.mysql-db-query

You should then be able to check the databases one at a time using two mysql_db_query() statements.

Code:
mysql_db_query("db1",$query);
mysql_db_query("db2",$query);
For multiple servers, its best to give each connection its own handle and refer to the handle when processing the query:

Code:
$handle_db1 = mysql_connect("localhost","myuser","apasswd");
$handle_db2 = mysql_connect("127.0.0.1","myuser","apasswd");
mysql_select_db("db1",$handle_db1);
mysql_select_db("db2",$handle_db2);
$query = "select * from test"; $which = $handle_db1;
mysql_query($query,$which);
__________________
~==================================~
Kim Kraft
StudioKraft
http://www.studiokraft.com/
~==================================~
  #3 (permalink)  
Old 05-17-2007, 08:15 PM
jazzmatazz2005 jazzmatazz2005 is offline
WebProWorld Member
 

Join Date: Aug 2003
Location: Jersey City, NJ
Posts: 71
jazzmatazz2005 RepRank 0
Default

thanks studiokraft

I think i understand what you talking about but can you show me useing the code i provided here.
__________________
It's better to do business with me than against me!
  #4 (permalink)  
Old 05-17-2007, 09:26 PM
studiokraft studiokraft is offline
WebProWorld New Member
 

Join Date: Jun 2006
Location: NorthEast
Posts: 23
studiokraft RepRank 0
Default

Quote:
Originally Posted by jazzmatazz2005
I think i understand what you talking about but can you show me useing the code i provided here.
Uh, sure, I guess...

<?php
session_start();
if( isset($_POST['login']) && !isset($_SESSION['fname'])){
mysql_connect( 'localhost', 'root', '' );
$query = "SELECT * FROM `ims` WHERE `email` = '".mysql_real_escape_string($_POST['user'])."' AND `password` = '".mysql_real_escape_string($_POST['pass'])."'";
if ($result = mysql_db_query("login",$query)) {
if (mysql_num_rows($result)) {
$row = mysql_fetch_array($result);
$_SESSION['fname'] = stripslashes($row['fname']);
}
}
if (!isset($row['fname'])) {
if ($result2 = mysql_db_query("login2",$query)) {
if (mysql_num_rows($result2)) {
$row = mysql_fetch_array($result2);
$_SESSION['fname'] = stripslashes($row['fname']);
}
}
}
$header = (isset($row['fname'])) ? 'welcome' : 'errlogin';
header("Location: " . $header . ".php");
}
?>
__________________
~==================================~
Kim Kraft
StudioKraft
http://www.studiokraft.com/
~==================================~
  #5 (permalink)  
Old 05-18-2007, 03:00 PM
Vectorman211's Avatar
Vectorman211 Vectorman211 is offline
Moderator
WebProWorld Moderator
 

Join Date: Jul 2006
Posts: 89
Vectorman211 RepRank 2
Default

Great answer! Topic locked.
__________________
http://www.wis-tech.net
Closed Thread

  WebProWorld > Webmaster, IT and Security Discussion > Database Discussion Forum
Tags: databases



Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



Search Engine Friendly URLs by vBSEO 3.0.0