Jump to content

[SOLVED] Multiple Mysql Connections on one Page


mikePhp

Recommended Posts

Hiya

 

i am currently trying to access 2 different DB's on the same host, but they have different pass's and user names, on the same page.

 

I am getting a connection, But the second connection is OVERWRITING the first Query, so i am only seeing the display on the mainpage of the second DB Query

 

I am putting the INFO in to ARRAYS and then calling them I.e

 

$database = array('db1','db2');
$user =array('user1','user2');
$pass = array('pass1','pass2');


for($i=0; $i<count($database) && $i<count($user) && $i<count($pass); $i++)
{
  $connection = mysql_connect($hostname, $user[$i], $pass[$i]);
}

if(!connection)
{
$noCon = 'Sorry there has been an issue connecting to the database';
}
else
{
   for($i=0; $i<count($database); $i++)
  { 
    mysql_select_db($database[$i], $connection);
  }
}

 

Now i did try the:

 

 foreach($user as $newUser)
{
}

 

but i needed to get the rest of the foreach's then if you get me...

 

 

I hope you can help in some sort of way matey

 

cheers

Mike

 

 

Link to comment
Share on other sites

$link = mysql_connect($h, $u, $p) or die(mysql_error());

$link2 = mysql_connect($h2, $u2, $p2) or die(mysql_error());

 

example: mysql_query($query, $link);

 

----------------

Now playing: Red Hot Chili Peppers - Breaking The Girl

via FoxyTunes

 

Cheers Matey,

 

Tried that but doesn't seem to work....

 

This is what i put.

 

$database = array('db1','db2');
$user =array('user1','user2');
$pass = array('pass1','pass2');



  $connection = mysql_connect($hostname, $user[0], $pass[0]);
  $connection2 - mysql_connect($hostname, $user[1], $pass[1]);


if((!connection) || (!$connection2))
{
$noCon = 'Sorry there has been an issue connecting to the database';
}
else
{
    mysql_select_db($database[0], $connection);
    mysql_select_db($database[1], $connection2);
  }
}

 

I have separate queries for either connection,

 

But only one of the queries has completed,

 

Do you thing one is overwriting the other...??

 

Cheers

Mike

Link to comment
Share on other sites

$connection2 - mysql_connect($hostname, $user[1], $pass[1]);

$connection2 = mysql_connect($hostname, $user[1], $pass[1]);

 

might have just been a typo here on the forums but be sure that that '-' is a '=' in your code.

Link to comment
Share on other sites

$connection2 - mysql_connect($hostname, $user[1], $pass[1]);

$connection2 = mysql_connect($hostname, $user[1], $pass[1]);

 

might have just been a typo here on the forums but be sure that that '-' is a '=' in your code.

 

yea matey, just a typo  ::)

 

I cant seem to get both connections on the same page to display there quiers, one seems to overwrite the other...??

 

 

Link to comment
Share on other sites

You are going to need to post the relevant code. It is likely that you are reusing a variable someplace, resulting in queries overwriting each other.

 

 

The relevant code was in my first post mate, i was using aray's to get through the process, but i think it is connecitng to the first connection and then overwriting on the second connection.

Link to comment
Share on other sites

The code in reply #2 (once the errors in it are corrected - there is also a missing $ on one of the variable names in the if() test and an extra }) works and successfully creates connections to two separate databases and allows two different queries to  work (just tested.) So, something specific in your code is causing the problem. To get specific help with your code, you are going to need to post it.

Link to comment
Share on other sites

Assuming the code above works to create two new connections, the only thing that I can think of that could be wrong is you're not including the link identifier in the arguments of mysql_query (i.e. mysql_query("SELECT * FROM something", $connection))...the 'rest' of the code (from the creation of the connections to the all the way up to the output) would help...

Link to comment
Share on other sites

right this is the code what i have got

 

I am calling am function in

 

//INCLUDE CREDENTIALS
include('libaries/config.php');

function dbConnect($hostname, $user, $pass, $database) 
{


		//COUNT THE CONNECTIONS DATABASES AND CREDENTIALS
		for($i=0; $i<count($user) && $i<count($pass) && $i<count($database); $i++)
		{
			$connection[$i] = mysql_connect($hostname, $user[$i], $pass[$i]);

			if(!$connection[$i])
			{
			echo "Sorry there has been a Database error " . mysql_error();
			mysql_close($connection[$i]);
			}
			else
			{
			mysql_select_db($database[$i], $connection[$i]);
			}
		}


}

 

My Index.php is the main page for all my pages, it calls the content of the rest of my pages,

 

http://www.planet-group.co.uk/magazines/foodanddrink/index.php

 

you will understand and these are the queries from the home page..

 

//START IF MYSQL
$query = mysql_query("SELECT id, title, caption, text, thumbName FROM planetFDBrochure ORDER BY id DESC LIMIT 0,1");	
list($id, $title, $caption, $text, $thumbnail) = mysql_fetch_array($query);


$rodEdit = mysql_query("SELECT ID, post_content, post_title, post_name FROM wp_posts ORDER BY ID DESC");
list($wpID, $wpContent, $wpTitle, $wpName) = mysql_fetch_array($rodEdit);

 

have a look....??

 

 

Link to comment
Share on other sites

The mysql_query definition from the php manual -

 

resource mysql_query ( string $query [, resource $link_identifier] )

 

mysql_query() sends an unique query (multiple queries are not supported) to the currently active database on the server that's associated with the specified link_identifier.

 

Parameters

 

query

A SQL query

 

The query string should not end with a semicolon.

 

link_identifier

The MySQL connection. If the link identifier is not specified, the last link opened by mysql_connect() is assumed. If no such link is found, it will try to create one as if mysql_connect() was called with no arguments. If by chance no connection is found or established, an E_WARNING level warning is generated.

 

To query on a specific connection, you must specify the link identifier in the mysql_query. Your code is not doing this. You are also making the connection inside of a function, which you previously did not show, so the $connection variables/array that is holding the link identifier only exists inside of that function.

 

Had you posted real code two days ago you could have had a solution sooner.

Link to comment
Share on other sites

Apologies for not getting the correct code in,

 

I didn't think it made much of a difference it been in a function to not been,

 

Any way the code works fine now thanks,

 

Thanks for all your time and effort and Inconvenience

 

Mike

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.

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