473,692 Members | 2,369 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

14 New Member

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 4097
5,058 Recognized Expert Expert

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;
  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.     }
  13.     public static function query($sql)
  14.     {
  15.         if(is_null(self::$dbLink1) || is_null(self::$dbLink2)) {
  16.             self::connect();
  17.         }
  19.         $result1 = self::$dbLink1->query($sql);
  20.         $result2 = self::$dbLink2->query($sql);
  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.)

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
14 New Member
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(dat a) 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
1,168 Recognized Expert Top Contributor
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
5,058 Recognized Expert Expert
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

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? When? Why? Thanks you for your answers.
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 database. I wonder how i could connect a person to several other id's in a table. I wonder if I can make a string in the database that holds all the id's separated with , for example....?
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 one column per element, each column is VARCHAR Does MySQL has anything special for XML data? Is there any software which helps to store XML data in MySQL
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 using as there are many choices to choose from. My database will contain a lot of records. TIA
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 can't figure out the coding to accomplish this. I have an Excel application that is a monster and it has become too much to maintain and test. I didn't write it but i support it. I am trying to convert this application to Access and it is not as...
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 several different data standards and schema. I'm finding it confusing to keep it all straight. What's this field for? Which standards document is it based on? What are the permitted values? Which business rule governs it? In theory, we do have all...
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: The goal of this tutorial is to design a Data Abstraction Layer (DAL) in PHP, that will allow us to ignore the intricacies of MySQL and focus our attention on our Application Layer and Business Logic. Hopefully, by the end of this guide, you will...
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 segregated data prep into a separate process to avoid excessive memory use at run time. Turns out python For the site I'm at, I've got 10 years sales history recapped from
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 different issue than what I was initially having. I am working on storing data collected from a form on my website. I would like the information to be stored into MySQL once entered by users. I have googled this question and have tried multiple...
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.