Jump to content

Pulling user/pass from a different database to store in a session.


Lostnode
 Share

Recommended Posts

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?

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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); 

Link to comment
Share on other sites

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...

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.