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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sganeshsvk
    New Member
    • Oct 2009
    • 14

    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
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    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.)
    [code=php]<?php
    class Database
    {
    private static $dbLink1;
    private static $dbLink2;

    private static function connect()
    {
    self::$dbLink1 = new mysqli("localho st", "user" ,"pw", "database") ;
    self::$dbLink2 = new mysqli("mysql.e xample.com", "user", "pw", "database") ;
    }

    public static function query($sql)
    {
    if(is_null(self ::$dbLink1) || is_null(self::$ dbLink2)) {
    self::connect() ;
    }

    $result1 = self::$dbLink1->query($sql);
    $result2 = self::$dbLink2->query($sql);

    if(!$result1) {
    user_error("Fai led to execute query on local SQL server.", E_USER_WARNING) ;
    }
    if(!$result2) {
    user_error("Fai led to execute query on remote SQL server.", E_USER_WARNING) ;
    }
    }
    }
    ?>[/code]

    Which would allow you to just do:
    [code=php]Database::query ("INSERT INTO whatever(col) VALUES('val1', 'val2')");[/code]
    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.

    Comment

    • sganeshsvk
      New Member
      • Oct 2009
      • 14

      #3
      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...

      Comment

      • TheServant
        Recognized Expert Top Contributor
        • Feb 2008
        • 1168

        #4
        Originally posted by sganeshsvk
        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...
        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.

        Comment

        • Atli
          Recognized Expert Expert
          • Nov 2006
          • 5062

          #5
          Originally posted by sganeshsvk
          But i want to store the data values in two different databases
          in mysql at the same time only.
          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?

          Comment

          Working...