How to insert data in two different tables of two different database in php

前端 未结 5 841
醉酒成梦
醉酒成梦 2021-01-21 09:35

I have to insert data in two different database\'s table. I have created database1 and table1 for database1, also i have created database2 and table2 for database2.

For

相关标签:
5条回答
  • 2021-01-21 09:52

    Well, that's how i do it...

    1 - connect --> that you are doing right 2 - check for errors 3 - USE a database (1) you want to put data in (and not 'SELECT') 4 - check for errors 5 - now INSERT items into the database THAT IS BEING USED - that is (1) 6 - check for errors 7 - USE the other database (2) 8 - check for errors 9 - INSERT the data into (2) - because that is the one in use now 10 - check for errors

    Yes, be paranoid :P Hope this helps

    0 讨论(0)
  • 2021-01-21 09:55

    Simply connect to 1 database, insert new row, disconnect, connect to the other database, insert row into that one and disconnect.

    Or you can use $connect1 and $connect2 to refer to each of them separately and do the insertion parallely.

    EDIT: Btw you can select the database with the 4'th parameter of mysql_connect, no need to use mysql_select_db

    And very important, you should write mysqli not mysql. Because mysql functions are not going to be supported for much longer.

    0 讨论(0)
  • 2021-01-21 09:57

    Try the following code:

    $connect1 = mysql_connect("localhost","root","");
    mysql_select_db("database1", $connect1);
    $res1 = mysql_query("query",$connect1);
    
    $connect2 = mysql_connect("localhost","root","",true);
    mysql_select_db("database2", $connect2);
    $res2 = mysql_query("query",$connect2);  
    

    Note: So mysql_connect has another optional boolean parameter which indicates whether a link will be created or not. as we connect to the $connect2 with this optional parameter set to 'true', so both link will remain live.

    0 讨论(0)
  • 2021-01-21 10:06

    Well, if there's a pattern in db names, tables and queries are exactly the same, you can use a loop:

    for ($i = 1; $i <=2; $i++) {
        mysql_select_db("database".$i, $connect); 
        $sql = mysql_query("INSERT INTO table".$i." (contact_first, contact_last, contact_email) VALUES('abc','xyz','abc@abc.com')");
        mysql_close;
    }
    

    However, using mysql_* is strongly NOT recommended, as it is deprecated from the last stable PHP release and is considered unsafe. Use PDO or MySQLi instead. PHP's official site suggests the article "Choosing an API": http://www.php.net/manual/en/mysqlinfo.api.choosing.php

    0 讨论(0)
  • 2021-01-21 10:11

    first create two database connections

    $connect1 = mysql_connect("localhost","root",""); 
    $connect2 = mysql_connect("localhost","root",""); 
    

    Then select the database for each connection.

    mysql_select_db("database1",$connect1); // select database1 
    mysql_select_db("database2",$connect2); // select database2 
    

    Then pass in a second argument for mysql_query which is the respective connection for the query.

    mysql_query("SELECT ... ", $connect1);
    mysql_query("SELECT ... ", $connect2);
    
    0 讨论(0)
提交回复
热议问题