PDA

View Full Version : Two databases help


Crisp
10-22-2009, 07:24 PM
I want to connect two databases, but I can't.
This is what I have:
<?php
mysql_connect("localhost","root");
$mybbDb = mysql_select_db("mybb2");
//RevBB
$revbbDb = mysql_select_db("newbb");
$users = mysql_query(
"SELECT `uid`
FROM `$mybbDb.myf_users`"
)or die(mysql_error());
while($user = mysql_fetch_array($users))
{
echo $user['uid'];
}

First off, I'm not using the Newbb database, and I don't need to right yet.
The problem is, I can't connect to the MyBB2 database. Gives me:
Incorrect table name '1.myf_users'

Can someone help me?
Thanks.

Fumigator
10-22-2009, 07:39 PM
The mysql_select_db() function doesn't return the name of a database, it returns TRUE or FALSE. When you append a boolean to a string as you're doing, you get "1" for true and "0" for false. Thus the table name '1.myf_users'.

You really don't actually have to even use the mysql_select_db() function at all. If you want to explicitly reference a database in a query, you simply prefix the table name with the database.


SELECT `uid`
FROM mybb2.myf_users

Crisp
10-22-2009, 07:42 PM
Here's what I've got:
<?php
mysql_connect("localhost","root");
$users = mysql_query(
"SELECT *
FROM
mybb2.myf_users."
)or die(mysql_error());
$user = mysql_fetch_array($users);
?>
And gives me:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '.' at line 3

When I have it like this:
<?php
mysql_connect("localhost","root");
$users = mysql_query(
"SELECT *
FROM
".mybb2.myf_users.""
)or die(mysql_error());
$user = mysql_fetch_array($users);
?>
I get this:
No database selected

Usually I know these problems, but I guess my mind is blank atm.
Thanks.

EDIT: I've got it fixed now. Thanks dude.

Crisp
10-22-2009, 11:28 PM
Question, how do I connect two users to one of them? Thanks.

abduraooft
10-23-2009, 09:57 AM
Question, how do I connect two users to one of them? Thanks.
You may create another use with right privileges and connect to the DB, but may I ask, why you need this?