I wrongly designed my application to have one database to each user. each user had 3 similar tables. I now want to have one database and 3 tables only; where i will use the database name in the old databases as a reference in the new system. There was another database called "users" in the old database that stored the database names. I'm done with the schema design of the new database and now left with the migration.
The trick here is that I have 3 db connections. I first connect to the users database and userinfo table, pick up the database_name in a loop, use it to connect each old db and further connect to personal, accounts and games table.
After picking up the tables, i will like to populate/join it with the new Database (DATA_ONE) and the tables whiles i append the old database_name to the new tables. Any help on this or is there a better way to do this? Thanks much
<?php
$dbhost = "localhost";
$dbname = "users";
$dbuser = "root";
$dbpass = "*****";
$conn1 = mysql_connect($dbhost, $dbuser, $dbpass, TRUE) or die("MySQL Error: " . mysql_error());
$conn2 = mysql_connect($dbhost, $dbuser, $dbpass, TRUE) or die("MySQL Error: " . mysql_error());
$conn3 = mysql_connect($dbhost, $dbuser, $dbpass, TRUE) or die("MySQL Error: " . mysql_error());
mysql_select_db($dbname,$conn1) or die("MySQL Error: " . mysql_error());
$query = "SELECT database_name FROM userinfo";
$result1 = mysql_query($query,$conn1);
while($row = mysql_fetch_array($result1, MYSQL_ASSOC))
{
$database_name =$row['database_name'];
$conn2 = mysql_connect($dbhost, $dbuser, $dbpass) or die("MySQL Error: " . mysql_error());
$db = mysql_select_db($database_name ,$conn2) ;
// now, pick personal, accounts, games tables of user and populate new database and tables.
// this is where i need help.
}
?>
Why do you think you need 3 seperate database connections with the same credentials? In any mysql query you can reference tables from any database on the same instance by prefixing the table name with the database name (and a . in between). I'd suggest not using 'database_name' as an attribute name:
$databases=get_mysql('SELECT DISTINCT database FROM users.userinfo ORDER BY database');
$count_of_users=count($databases);
foreach ($databases as $user_offset=>$user) {
$uqry="INSERT INTO data_one.personal
(user, id, address, password)
SELECT '$user'
, (id*$count_of_users*$user_offset)
, address
, password
FROM ${user}.personal";
...
(assuming that 'id' is an autoincrement value which may be referenced elsewhere).
Related
help me please guys
I've searched and tried all the articles here
but still can not get results from INFORMATION_SCHEMA
I want to get a relationship between tables using KEY_COLUMN_USAGE but still not showing anything, just displaying a blank page
I do not understand what went wrong:
I've used the code I used to use, as below
$server = "localhost";
$user= "root";
$pass= "";
$db= "my_DB";
$conn = mysql_connect($server, $user, $pass) or die ("can't connect server" .mysql_error());
$openconn= mysql_select_db($db) or die ("can't open DB " .mysql_error());
$sql="using_select_command_here";
$query = mysql_query($sql) or die ("error:" .mysql_error());
while ($data = mysql_fetch_array($query)) {
print_r($data);
}
from all sources are here like :
How to find tables all relation in MySQL
How to know relations between tables
the result still showing blank page:
I do not understand if there are other factors?
I am using php version 5.3.1
and mysql version 5.1.41
from xampp for windows ver1.7.3
We have this multiple database and we need to connect all of them together. We are using relationship table to talk back and forth. Multiple database will help us to backup the project or version separately.
Registration database: User Log In
Project database: Number of projects under the Logged in user.
Version database: Number of versions under the selected Project
THE ISSUE
Because we are using 0 and 1, only one user can able to connect to the database at a time. What we need is like the one in the flowchart below. Multiple user can able to connect to the database and working on a different project > different versions.
Thanks!
Sidenote: We are open to try different approach
<?php
//database error message
$connect_error='We could not able to connect. Please try later';
// Registration table
$main = mysql_connect('localhost','root','',true) or die($connect_error);
mysql_select_db('registration-table', $main) ;
// Obtaining session id
if(isset($_SESSION['id'])===true){
$session = $_SESSION['id'];
// Filtering projects based on 0 and 1
$sql = mysql_query("SELECT * FROM project1_table where user_id = '$session' and database_active = '0'",$main);
$row = mysql_fetch_array($sql);
// Each Project has Versions
$project_id = $row['id'];
$sqli = mysql_query("SELECT * FROM version_table where project_id = '$project_id' and database_active ='0'",$main);
$emp = mysql_fetch_array($sqli);
// Fetching the database name
$db_name = $emp['database_name'];
$sub = mysql_connect('localhost','root','',true) or die($connect_error);
mysql_select_db("$db_name", $sub) ;
}
?>
First of all I would suggest you to use mysqli or PDO other then mysql , Because after PHP 5.5 version mysql function deprecated and therefore mysql function will not be available in future
Multiple DB connection in Mysql
$dbh1 = mysql_connect($hostname, $username, $password);
$dbh2 = mysql_connect($hostname, $username, $password, true);
mysql_select_db('database1', $dbh1);
mysql_select_db('database2', $dbh2);
mysql_query('select * from tablename', $dbh1);
mysql_query('select * from tablename', $dbh2);
Multiple connection using Mysqli
$link1 = new mysqli($hostname, $username, $password,$database1);
$link2 = new mysqli($hostname, $username, $password,$database2);
mysqli_query($link1,"SELECT * FROM table");
mysqli_query($link2,"SELECT * FROM table");
Multiple DB connection in PDO
$conn1 = new PDO("mysql:host=$hostname;dbname=database1", $username, $password);
$conn2 = new PDO("mysql:host=$hostname;dbname=database1", $username, $password);
$conn1->query("SELECT * FROM table");
$conn1->query("SELECT * FROM table");
$conn1 = mysqli_connect( $db_host, $db_user, $db_pass, $db_name );
$conn2 = mysqli_connect( $db_host2, $db_user2, $db_pass2, $db_name2 );
Let's say you have a database named chat1 and chat2, and a table named tbl_chat1 and tbl_chat2.
$sql1 = "SELECT `id`, `msgs` FROM `chat1` . `tbl_chat1`";
$sql2 = "SELECT `id`, `msgs` FROM `chat2` . `tbl_chat2`";
$result1 = mysqli_query($conn1, $sql1);
$result2 = mysqli_query($conn2, $sql2);
However you can achieve the backup of your database without having to connect to multiple databases OK.
I need to write a script which will take the values from two columns and use them to update the column in a view that I created in another database. In the first database I have sku and qty as well as in the view.
here is my code:
$server = 'localhost';
$user = 'invodata';
$pass = 'Abcd1234!1';
$dbname = 'tboinvodata';
$con = mysql_connect($server, $user, $pass) or die("Can't connect");
mysql_select_db("tboinvodata") or die(mysql_error());
$result = mysql_query("SELECT item, onhand FROM immaster"); <- this is getting the values from the columns in the first data base
$server = 'localhost';<-setting up my second connection to other database
$user = 'tbo';
$pass = 'Abcd1234!1';
$dbname = 'i187358_mage1';
$con = mysql_connect($server, $user, $pass) or die("Can't connect");
mysql_select_db("i187358_mage1") or die(mysql_error());
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {<-this gets the array from other database
UPDATE qtyview SET qty = $row["onhand"] WHERE sku = item;<- this should update the necessary columns "sku" is used in my view and "item" is used in the first data base I use this so the proper rows in the other columns get updated.
}
?>
really not sure what I am doing wrong I am pretty new to this though.
You can make multiple calls to mysql_connect() and use them like this.
First connect to two your MYSQL USER
$con1 = mysql_connect($server, $user, $pass);
$con2 = mysql_connect($server, $user, $pass, true);
Then establish a connect with different DATABASE
mysql_select_db('firstdatabase', $con1);
mysql_select_db('seconddatabase', $con2);
Then query from firstdatabase like this
mysql_query('select * from views', $con1);
And query from seconddatabase
mysql_query('select * from views', $con2);
This code is not tested by me...but i think it will work good for you.. :)
I made a SQL query in my database and it works perfectly, but when I try to put this same query into PHP, it doesn't work. I cannot figure out where is the error.
//connection variables
$host = "localhost";
$database = "kjnjkyeo3";
$user = "root";
$pass = "probajovo11";
//connection to the database
$connection = mysql_connect($host, $user, $pass, $database)
or die ('cannot connect to the database: ' . mysql_error());
$sql = "SELECT ps_orders.id_order, ps_order_detail.id_order, ps_order_detail.product_reference AS Itemno, ps_order_detail.product_quantity AS Saleqty, ROUND(ps_order_detail.total_price_tax_incl, 2) AS Cost, DATE_FORMAT(ps_orders.date_add , \'%Y%m%dT%T\' ) AS Dateoftrans\n"
. "FROM ps_orders\n"
. "LEFT JOIN kjnjkyeo3.ps_order_detail ON ps_orders.id_order = ps_order_detail.id_order";
//loop to show all the tables and fields
$loop = mysql_query($sql)
or die ('cannot select tables');
I made a lot of changes in my query but I always get the message cannot select tables. When I make a simple query like Select tables from $database, it works fine.
I made the changes:
//connection variables
$host = "localhost";
$database = "kjnjkyeo3";
$user = "root";
$pass = "probajovo11";
//connection to the database
$connection = mysql_connect($host, $user, $pass)
or die ('cannot connect to the database: ' . mysql_error());
//select the database
mysql_select_db($database)
or die ('cannot select database: ' . mysql_error());
$sql = "SELECT ps_orders.id_order, ps_order_detail.id_order, ps_order_detail.product_reference AS Itemno, ps_order_detail.product_quantity AS Saleqty, ROUND(ps_order_detail.total_price_tax_incl, 2) AS Cost, DATE_FORMAT(ps_orders.date_add , \'%Y%m%dT%T\' ) AS Dateoftrans FROM ps_orders LEFT JOIN ps_order_detail ON ps_orders.id_order = ps_order_detail.id_order";
//loop to show all the tables and fields
$loop = mysql_query($sql)
or die ('cannot select tables');
but it still doesn't work
$connection = mysql_connect($host, $user, $pass) or die ('cannot connect to the database: ' . mysql_error());
mysql_select_db($database);
$sql = "SELECT ps_orders.id_order, ps_order_detail.id_order,
ps_order_detail.product_reference AS Itemno, ps_order_detail.product_quantity AS Saleqty, ROUND(ps_order_detail.total_price_tax_incl, 2) AS Cost, DATE_FORMAT(ps_orders.date_add , \'%Y%m%dT%T\' ) AS Dateoftrans FROM ps_orders LEFT JOIN kjnjkyeo3.ps_order_detail ON ps_orders.id_order = ps_order_detail.id_order";
mysql_connect doesn't allow you to select the database like you are doing. You have to use mysql_select_db and one day you'll also have to eventually move to mysqli or PDO.
$connection = mysql_connect($host, $user, $pass) or die ('cannot connect to the database: ' . mysql_error());
mysql_select_db($database);
mysql_connect does not take a $database parameter. http://dk1.php.net/manual/en/function.mysql-connect.php, instead use:
$connection = mysql_connect($host, $user, $pass)
mysql_select_db($database, $connection)
Use mysql_select_db($database) to tell on which databases will the queries run.
If you are selecting from multiple databases you can use this way of referecing a field
`database_name`.`table_name`.`field_name`
or just
`database_name`.`table_name`
for a table in FROM.
mysql_* functions are deprecated, if you are starting a new project use mysqli or pdo
I have databases of users and each has tables. I want to loop through each user and find the number of rows of a particular table common to each. So i connect to the first DB(usersDB) and pick the names of other DB's from a table(userinfo) row(user_name). I then connect to each DB using the names obtained in userinfo and try to find the number of rows they each have on a particular table(products) common to them. I tried this but shows the same number of rows for all of them. Any help??
<?php
//db parameters
$dbhost = "localhost";
$dbname = "usersDB";
$dbuser = "root";
$dbpass = "";
mysql_connect($dbhost, $dbuser, $dbpass) or die("MySQL Error: " . mysql_error());
mysql_select_db($dbname) or die("MySQL Error: " . mysql_error());
//select main db
$query = "SELECT user_name FROM userinfo";
$result = mysql_query($query);
while($row = mysql_fetch_array($result, MYSQL_BOTH))
{
$dbName =$row['user_name'];
mysql_connect($dbhost, $dbuser, $dbpass) or die("MySQL Error: " . mysql_error());
mysql_select_db("dbprefix_".$bName) or die("MySQL Error: " . mysql_error());
// do a query for each db
$query = mysql_query('SELECT * FROM `products`');
$num_rows = mysql_num_rows($query);
echo $dbName." has".$num_rows."products"."<br/>";
}
?>
I think problem is in following line
mysql_select_db("dbprefix_".$bName) or die("MySQL Error: " . mysql_error());
I think this line will be
mysql_select_db("dbprefix_".$dbName) or die("MySQL Error: " . mysql_error());
this may not be your issue but I noticed you arn't closing the connection to each database after you query from it. you should assign a variable to mysql_select_db and after you echo close the database like this:
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die("MySQL Error: " . mysql_error());
while($row = mysql_fetch_array($result, MYSQL_BOTH)){
$dbName =$row['user_name'];
$db = mysql_select_db("dbprefix_".$dbName, $conn) or die("MySQL Error: " . mysql_error());
if( $db ){
// do a query for each db
$query = mysql_query('SELECT * FROM `products`');
$num_rows = mysql_num_rows($query);
echo $dbName." has".$num_rows."products"."<br/>";
mysql_close( $db );
}
}
also notice I took the mysql_connect() line out of the while loop because you don't need to call this more than once. and I added the $conn variable for your mysql_connect() command, this way you can use $conn in your mysql_select_db() statement. This tell the select_db statement which connection to look in for this database (just alittle more secure).
Seems that there is a typo here:
mysql_select_db("dbprefix_".$bName) or die("MySQL Error: " . mysql_error());
Did you mean "dbprefix_".$dbName instead of $bName?
You don't need to call
mysql_connect($dbhost, $dbuser, $dbpass) or die("MySQL Error: " . mysql_error());
every time, just call mysql_select_db for each database and PHP will reuse the connection
This is a copy paste from php manual may be it helps you
If you use implode() with the return value by mysql_fetch_array,
if you use MYSQL_BOTH on parameter 2, the result is not really what you're expecting.
For example :
my sql database contains "Amine, Sarah, Mohamed";
$array = mysql_fetch_array($resource,MYSQL_BOTH);
or $array = mysql_fetch_array($resource);
echo implode(" - ", $array);
the result is : Amine-Amine-Sarah-Sarah-Mohamed-Mohamed
and we expect just : Amine-Sarah-Mohamed
You must use MYSQL_NUM or MYSQL_ASSOC on parameter 2 to resolve the problem.
Seems rather inefficient to start up a new connection, using the same user/password for every user you've got. MySQL is perfectly capable of querying across different databases from the same connection:
mysql_connect(...);
$sql = "SELECT user_name FROM userinfo";
$result = mysql_query($sql) or die(mysql_error()) {
while($row = mysql_fetch_assoc($result)) {
$username = $row['user_name'];
$sql2 = "SELECT count(*) AS cnt FROM dbprefix_{$username}.products";
$result2 = mysql_query($sql2) or (die(mysql_error());
echo "$username has {$result2['cnt']} products";
}
In short, doing
SELECT somedb.sometable.somefield
is the same as doing
mysql_select_db('somedb');
SELECT sometable.somefield;