Lostnode Posted October 27, 2010 Share Posted October 27, 2010 Unfortunately I have no code for this yet cuz I don;t even know if its possible... I am programming an application that is used by a couple of stores, which could end up being a lot of stores. Anyways, the basis is that the stores would, though a separate application (and therefore separate database) create a username and password, I now want to use this username and password to do the following 1. Allow them to login to my application using the same username and password 2. I want the store the username in a session to pull tables based on the username from my database For instance, a user has the login store123, after loggin in it now pulls the information from the tables store123_items, store123_prices, store123_settings, etc. Now my database will have quite a lot of store###_tables I am, sadly, a noobie to PHP and I do recall seeing an article (somewhere on the net, and I stupidly forgot to bookmark it, knowing I would need it eventually) on how to access multiple databases easily. Now because they are both under my account I can use the same username and password for both, its accessing the MySQL username/password database and storing the info I know I am lacking on how to do it. Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/216969-pulling-userpass-from-a-different-database-to-store-in-a-session/ Share on other sites More sharing options...
Lostnode Posted October 27, 2010 Author Share Posted October 27, 2010 Ok, I found the article... http://www.php.net/manual/en/function.mysql-select-db.php#52584 but not sure how to implement it... My current connect.php file looks like this: <?php $hostname='localhost'; //// specify host, i.e. 'localhost' $user='user'; //// specify username $pass='pass'; //// specify password $dbase='database'; //// specify database name $connection = mysql_connect("$hostname" , "$user" , "$pass") or die ("Can't connect to MySQL"); $db = mysql_select_db($dbase , $connection) or die ("Can't select database."); ?> obviously the user, pass, and db have been changed for privacy reasons lol. Quote Link to comment https://forums.phpfreaks.com/topic/216969-pulling-userpass-from-a-different-database-to-store-in-a-session/#findComment-1126998 Share on other sites More sharing options...
trq Posted October 27, 2010 Share Posted October 27, 2010 Are you asking how to connect to multiple database servers? Make another call to mysql_connect() and store your connection in a different variable to $connection. Quote Link to comment https://forums.phpfreaks.com/topic/216969-pulling-userpass-from-a-different-database-to-store-in-a-session/#findComment-1127004 Share on other sites More sharing options...
Lostnode Posted October 27, 2010 Author Share Posted October 27, 2010 Are you asking how to connect to multiple database servers? Make another call to mysql_connect() and store your connection in a different variable to $connection. Thats part of it. I found a solution here - http://www.php.net/manual/en/function.mysql-select-db.php#52584 But I do not know how to implement it with my current connect.php script. (I usualyy fubb it up somehow) The other part is storing the username in a session so I only have to go to the DB once. Quote Link to comment https://forums.phpfreaks.com/topic/216969-pulling-userpass-from-a-different-database-to-store-in-a-session/#findComment-1127007 Share on other sites More sharing options...
Lostnode Posted October 27, 2010 Author Share Posted October 27, 2010 Ok, without trying it (as I am a noob, I would like confirmation from someone who actualy knows what they are doing before I kill my scripts) Instead of <?php $hostname='localhost'; //// specify host, i.e. 'localhost' $user='user'; //// specify username $pass='pass'; //// specify password $dbase='database'; //// specify database name $connection = mysql_connect("$hostname" , "$user" , "$pass") or die ("Can't connect to MySQL"); $db = mysql_select_db($dbase , $connection) or die ("Can't select database."); ?> Would I use: <?PHP $handle_db1 = mysql_connect("localhost","myuser","apasswd") or die ("Can't connect to MySQL");; $handle_db2 = mysql_connect("localhost","myuser","apasswd") or die ("Can't connect to MySQL");; $db1 = mysql_select_db("db1",$handle_db1) or die ("Can't select database."); $db2 = mysql_select_db("db2",$handle_db2) or die ("Can't select database."); ?> Which would also include the Error reporting which the article I linked does not show. I also believe that upon login from the first database and comparing the username and password I could use $_SESSION['username'] = $_REQUEST['username']; to store the info, then call it back using $dbprefix = $SESSION['username'] Which could them be used to pull info from the second database by using $query = "select * from " . $dbprefix . "_table"; $which = $handle_db2; mysql_query($query,$which); Am I at least on the right track? Quote Link to comment https://forums.phpfreaks.com/topic/216969-pulling-userpass-from-a-different-database-to-store-in-a-session/#findComment-1127023 Share on other sites More sharing options...
trq Posted October 27, 2010 Share Posted October 27, 2010 If (as your code suggests) both databases are on the same database server, you only need to make one connection. You then use mysql_select_db to switch between the two different servers. eg; <?php $handle = mysql_connect("localhost","myuser","apasswd"); mysql_select_db("db1",$handle); // code querying db1 mysql_select_db("db2",$handle); // more code that queries db2 mysql_select_db("db1",$handle); // again if need be, more code that queries db2 Quote Link to comment https://forums.phpfreaks.com/topic/216969-pulling-userpass-from-a-different-database-to-store-in-a-session/#findComment-1127038 Share on other sites More sharing options...
Lostnode Posted October 27, 2010 Author Share Posted October 27, 2010 If (as your code suggests) both databases are on the same database server, you only need to make one connection. You then use mysql_select_db to switch between the two different servers. eg; <?php $handle = mysql_connect("localhost","myuser","apasswd"); mysql_select_db("db1",$handle); // code querying db1 mysql_select_db("db2",$handle); // more code that queries db2 mysql_select_db("db1",$handle); // again if need be, more code that queries db2 Ok, now I am confused. The way I have it above (the way it suggested at PHP.net) was to use 2 different handles so that you would not have to use mysql_select_db() between databases every time. I think I might be over my head on this one. Quote Link to comment https://forums.phpfreaks.com/topic/216969-pulling-userpass-from-a-different-database-to-store-in-a-session/#findComment-1127179 Share on other sites More sharing options...
BlueSkyIS Posted October 27, 2010 Share Posted October 27, 2010 i prefer to use 2 different handles so i don't have to use mysql_select_db to switch back and forth. Quote Link to comment https://forums.phpfreaks.com/topic/216969-pulling-userpass-from-a-different-database-to-store-in-a-session/#findComment-1127218 Share on other sites More sharing options...
Lostnode Posted October 27, 2010 Author Share Posted October 27, 2010 i prefer to use 2 different handles so i don't have to use mysql_select_db to switch back and forth. Agreed, am I doing it right though? by using the following as my connect.php? <?PHP $handle_db1 = mysql_connect("localhost","myuser","apasswd") or die ("Can't connect to MySQL");; $handle_db2 = mysql_connect("localhost","myuser","apasswd") or die ("Can't connect to MySQL");; $db1 = mysql_select_db("db1",$handle_db1) or die ("Can't select database."); $db2 = mysql_select_db("db2",$handle_db2) or die ("Can't select database."); ?> then calling it by using: //from first database (for password and user info) $query = "select * from users; $which = $handle_db2; mysql_query($query,$which); //from second database (for all other inquiries) $query = "select * from " . $dbprefix . "_table"; $which = $handle_db1; mysql_query($query,$which); Quote Link to comment https://forums.phpfreaks.com/topic/216969-pulling-userpass-from-a-different-database-to-store-in-a-session/#findComment-1127234 Share on other sites More sharing options...
Lostnode Posted October 29, 2010 Author Share Posted October 29, 2010 Ok, tried it out and it does not work... So can someone PLEASE tell me if I am doing this right. I am trying to connect to 2 differetn databses, different username, different password, same host. Here is my connect.php file: <?PHP $handle_db1 = mysql_connect("localhost","user1","pass1") or die ("Can't connect to MySQL");; $handle_db2 = mysql_connect("localhost","user2","pass2") or die ("Can't connect to MySQL");; $db1 = mysql_select_db("db1",$handle_db1) or die ("Can't select database."); $db2 = mysql_select_db("db2",$handle_db2) or die ("Can't select database."); ?> Here is where I pull from the first database (located in Login.php): /* Verify that user is in database */ $q = "select password from m3_users where username = '$username'"; $which = $handle_db1; $result = mysql_query($q,$which); if(!$result || (mysql_numrows($result) < 1)){ return 1; //Indicates username failure } I get the error : Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /path/to/file/login.php on line 21 Line 21 is: $result = mysql_query($q,$which); Can anyone shed some light on this? I am confised, I am trying to follow the tutorial as per http://www.php.net/manual/en/function.mysql-select-db.php#52584 but it doesn;t seem to be working... at all... period... Quote Link to comment https://forums.phpfreaks.com/topic/216969-pulling-userpass-from-a-different-database-to-store-in-a-session/#findComment-1128089 Share on other sites More sharing options...
PFMaBiSmAd Posted October 29, 2010 Share Posted October 29, 2010 Is your code including the connect.php file? Are you closing the connection at any point prior to that code? You have posted just a few lines of your code taken out of context. That doesn't provide enough information to help you with what the rest of your code is doing that would cause the error. You need to post all your code from the first line of the main file being requested, all the way up to the line with the error, and any include/require files being used between those two points. Quote Link to comment https://forums.phpfreaks.com/topic/216969-pulling-userpass-from-a-different-database-to-store-in-a-session/#findComment-1128092 Share on other sites More sharing options...
Lostnode Posted October 29, 2010 Author Share Posted October 29, 2010 Simple enough. 3 FIles are being used: Connect.php (in the inc folder) <?PHP $handle_db1 = mysql_connect("localhost","user1","pass1") or die ("Can't connect to MySQL");; $handle_db2 = mysql_connect("localhost","user2","pass2") or die ("Can't connect to MySQL");; $db1 = mysql_select_db("db1",$handle_db1) or die ("Can't select database."); $db2 = mysql_select_db("db2",$handle_db2) or die ("Can't select database."); ?> login.php, in the main folder: <? /** * Checks whether or not the given username is in the * database, if so it checks if the given password is * the same password in the database for that user. * If the user doesn't exist or if the passwords don't * match up, it returns an error code (1 or 2). * On success it returns 0. */ function confirmUser($username, $password){ global $conn; /* Add slashes if necessary (for query) */ if(!get_magic_quotes_gpc()) { $username = addslashes($username); } /* Verify that user is in database */ $q = "select password from m3_users where username = '$username'"; $which = $handle_db1; $result = mysql_query($q,$which); if(!$result || (mysql_numrows($result) < 1)){ return 1; //Indicates username failure } Index.php in the main folder <? /* Include Files *********************/ session_start(); include("./inc/connect.php"); include("login.php"); /*************************************/ ?> <html> <title>Jpmaster77's Login Script</title> <body> <? displayLogin(); ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/216969-pulling-userpass-from-a-different-database-to-store-in-a-session/#findComment-1128096 Share on other sites More sharing options...
Lostnode Posted October 29, 2010 Author Share Posted October 29, 2010 Ok, I fixed it, seems I missed a line change... global $conn; becomes global $handle_db1; and it worked. Quote Link to comment https://forums.phpfreaks.com/topic/216969-pulling-userpass-from-a-different-database-to-store-in-a-session/#findComment-1128099 Share on other sites More sharing options...
PFMaBiSmAd Posted October 29, 2010 Share Posted October 29, 2010 You should be developing and debugging your code on a system with error_reporting set to E_ALL and display_errors set to ON so that all the php errors in your code will be reported and displayed. You will save a ton of time. The code using the connection is inside of a function. You need to pass the connection into the function as a parameter in the function call to make it available inside of the function. This will allow the function to be reused for any connection without needing to keep editing the function definition. The way the code is doing this now is using global $conn;, which you failed to take into account when you altered the code. Quote Link to comment https://forums.phpfreaks.com/topic/216969-pulling-userpass-from-a-different-database-to-store-in-a-session/#findComment-1128100 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.