473,320 Members | 1,910 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

How to store the same data values in two different databases in mysql using php ?

sir,

i want to store the same data values in two different databases at that same time in mysql using php programming.

suppose any one databases data will lose then we use the other databases.
suppose there is any query for store the same data values in two different databases at the same time.

for eg:

databases sample1

tables persons schools books


databases sample2

tables persons schools books
Oct 8 '09 #1
4 4076
Atli
5,058 Expert 4TB
Hey.

You just need to execute the same query twice, once for each connection.

You would just need to create a function that allows you to do that.
For example: (Note, I left out any sort of error checking for clarity.)
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. class Database
  3. {
  4.     private static $dbLink1;
  5.     private static $dbLink2;
  6.  
  7.     private static function connect()
  8.     {
  9.         self::$dbLink1 = new mysqli("localhost", "user" ,"pw", "database");
  10.         self::$dbLink2 = new mysqli("mysql.example.com", "user", "pw", "database");
  11.     }
  12.  
  13.     public static function query($sql)
  14.     {
  15.         if(is_null(self::$dbLink1) || is_null(self::$dbLink2)) {
  16.             self::connect();
  17.         }
  18.  
  19.         $result1 = self::$dbLink1->query($sql);
  20.         $result2 = self::$dbLink2->query($sql);
  21.  
  22.         if(!$result1) {
  23.             user_error("Failed to execute query on local SQL server.", E_USER_WARNING);
  24.         }
  25.         if(!$result2) {
  26.             user_error("Failed to execute query on remote SQL server.", E_USER_WARNING);
  27.         }
  28.     }
  29. }
  30. ?>
Which would allow you to just do:
Expand|Select|Wrap|Line Numbers
  1. Database::query("INSERT INTO whatever(col) VALUES('val1', 'val2')");
And it would be executed on both your servers.

Keep in mind tho that the queries are not executed at the exact same time, so if you are inserting times or dates, it would be advisable to create them use PHP rather then using the MySQL NOW() function. (Or any other such function.)


Buuuut...
It is worth mentioning that MySQL supports automatic replication.
See the documentation on Replication.

It's a bit more complex, but if you are doing something on a larger scale, it is worth looking into.
Oct 8 '09 #2
hello sir,

Your solution ok. But i want to store the data values in two different databases
in mysql at the same time only.

suppose we write the information(data) in bill vouchers, then we use carbon sheet at the same time we got the two copy of the same data records in your note.

Likely i want solution for this problem...
Oct 9 '09 #3
TheServant
1,168 Expert 1GB
@sganeshsvk
Did you read Atli's article on replication? Anyway, realistically nothing will ever be *exactly* the same time. The nano-seconds it takes to move to a next line render anything occuring at exactly the same time impossible (even on two different servers the chances are about 0). If you take a timestamp using PHP then make it a variable, you can insert that into the time/date field of your two databases, and that will say the same time.

If you don't use PHP for your timestamp rather than MySQL, you need to either use mysql replication function, or backup your database to another manually.

Personally I would use Atli's code and go with PHP looking after my timestamps, as replication is a bit fiddly.
Oct 9 '09 #4
Atli
5,058 Expert 4TB
@sganeshsvk
So you want the query executed on two separate database on the same MySQL server?

I don't see a way to do that with a single, plain, SQL query, but you could of course do this using a procedure or possibly a trigger.

However, that is basically the same thing being done by my previous PHP example, just coded inside MySQL rather than PHP. The idea is pretty much the same. (PHP is much more flexible, tho)

Or am I not getting what you are talking about?
Oct 9 '09 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: cover | last post by:
The question is, we have two options to store images, either in a Database (MySQL, Postgres, ...) like blob data, or in the hard disk the file and the path in database. Which option is better?...
4
by: Christopher Brandsdal | last post by:
Hi! I have a delicatg problem.... I have made a registration form for adding my friends information in a database. The problem is that I want to connect the persons with companies in the same...
12
by: jacob nikom | last post by:
Hi, I would like to store XML files in MySQL. What is the best solution: 1. Convert it to string and store it as CLOB/text 2. Serialize it and store as byte array 3. Flatten it out and create...
5
by: Roy Gourgi | last post by:
Hi, I am used to working in Visual FoxPro and I would like to be able to create a database and store and retrieve information from it. What is the simplest way to do it and what should I be...
11
by: Tim Hunter | last post by:
Hi I am using WinXP and Access 2003 Is it possible to store the field names of a table in an array and then loop through the array and update the table using the field names stored in the array? I...
1
by: Eric Sadoyama | last post by:
I have a database documentation question, but I am not even sure how to phrase it properly so I don't know where to start looking for answers. We are developing a database that is based on...
17
Motoma
by: Motoma | last post by:
This article is cross posted from my personal blog. You can find the original article, in all its splendor, at http://motomastyle.com/creating-a-mysql-data-abstraction-layer-in-php/. Introduction:...
0
by: Emile van Sebille | last post by:
Laszlo Nagy wrote: Hmm... I wrote an browser based analysis tool and used the working name pyvot... I found Numeric to provide the best balance of memory footprint and speed. I also...
70
mideastgirl
by: mideastgirl | last post by:
I have recently been working on a website for an honors association, and have a lot of difficulty but have found help from those on this site. I would like to see if I can get some more help on a...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.