How to use multiple database using php

Started on the old forum in April 2012 · view the original in the archive · old links redirect here

#database #multiple #php

3 replies · 497 views · started 14 years ago · 1 min read

3 participants
MuzicStar Newbie · 9 posts #1

plzz php/mysql master help me!!!

<?php 
    $conn = mysql_connect ("localhost", "root", ""); 
    $db1  = mysql_select_db ("database1", $conn); 
    $db2  = mysql_select_db ("database2", $conn); 

    $result = mysql_query ("SELECT * FROM whatever", $db1); 
?>

but i got this error msg: Access denied for user 'star'@'localhost' (using password: NO) plz correct this coding. thankx in advance

Loony Senior · 258 posts #2

You can make multiple calls to mysql_connect(), but if the parameters are the same you need to pass true for the '$new_link' (fourth) parameter, otherwise the same connection is reused.

so then you have

$dbh1 = mysql_connect($hostname, $username, $password); 
$dbh2 = mysql_connect($hostname, $username, $password, true); 

mysql_select_db('database1', $dbh1);
mysql_select_db('database2', $dbh2);

Then to query database 1, do

mysql_query('select * from tablename', $dbh1);

and for database 2

mysql_query('select * from tablename', $dbh2);

found this on google copy and pasted only took 4 minutes.

Creator of Epix.Mobi

Keep an Eye on us Big things coming soon!!!! Need something for your site hit me up here http://coding-talk.com/forum/main-forums/marketplace/153110-free-features-for-your-wapsite

just_m3. Developer · 199 posts #3

Loony wrote: $dbh2 = mysql_connect($hostname, $username, $password, true); found this on google copy and pasted only took 4 minutes.

yup "true" allows you to connect different database's in same script , without it .. connect cannot be done , because data's cannot be differentiate !

stackoverflow 😊 ...

This is ten percent luck, twenty percent skill Fifteen percent concentrated power of will Five percent pleasure, fifty percent pain 😂And a hundred percent reason to remember the name!

MuzicStar Newbie · 9 posts #4
<?php
$dbh1 = mysql_connect($hostname, $username, $password);
$dbh2 = mysql_connect($hostname, $username, $password, true);
$hostname="localhost";
$username="star_zedge";
$password="";

mysql_select_db('star_mstar', $dbh1);
mysql_select_db('star_admin', $dbh2);  
$sql = mysql_query('select * from mobile', $dbh2); 
//$sql = mysql_query ("SELECT * FROM mobile ORDER BY id DESC limit 5",$dbh1);
$result = mysql_query($sql) or die(mysql_error());
while ($row = mysql_fetch_assoc($result)) {  

	echo"<br/>";
	echo"{$row['mname']}";
        echo"<br/>";
}
?>

i have try this code but this code also not working.

Added after 21 minutes:

<?php
$conn1 = mysql_connect("localhost","USERNAME","PASSWORD") or die(mysql_error());
$conn2 = mysql_connect("localhost","USERNAME","PASSWORD","true") or die(mysql_error());

mysql_select_db("DB NAME1",$conn1);
mysql_select_db("DB NAME2",$conn2);

$query = "SELECT * FROM mobile";
$result = mysql_query($query,$conn1);
while ($row = mysql_fetch_assoc($result)) {  

	echo"<hr/>SHOW FROM 1ST DATABASE<br/>";
	echo"{$row['mname']}";
        echo"<br/>";
}
$result2 = mysql_query($query,$conn2);
while ($row = mysql_fetch_assoc($result2)) {  

	echo"<hr/>SHOW FROM 2ND DATABASE<br/>";
	echo"{$row['mname']}";
        echo"<br/>";
}
?>

This code working fine 🙂 Thankx for helping me.

Sign in to reply.

Similar topics

Keyboard shortcuts

g f
Go to forums
g h
Go to your feed
g m
Go to messages
r
Reply (in a topic)
/
Search
j
Next post
k
Previous post
?
This list
Ctrl/⌘ + Enter
Send what you are writing
Ctrl/⌘ + B / I
Bold / italic while writing
Esc
Close