将数据插入同一实例和同一用户的不同数据库的两个表中

时间:2016-04-06 14:27:32

标签: php mysql

我目前在phpfox_user和phpfox_user_custom中的brokers3_finalmain数据库中插入以下数据,但现在我想同时在其他数据库移动中插入数据full_name和country_iso。

public function delete_mobile($data)
{
    $params = array( ':mobile' => $data);
    $sql = "Delete FROM mobile where mobile=:mobile";
    $stmt = parent::query($sql, $params);

}

public function update_profile($data)
{
    $sql = 'UPDATE phpfox_user set full_name=:full_name, email=:email where user_id=:user_id';
    $params =(array(
        ':full_name' => $data['full_name'],
        ':email' => $data['email'],
        ':user_id' => $data['user_id']              
    ));
    //print_array($params);

    parent::query($sql, $params, '', 'main') or die(parent::$dbh->errorInfo());
}

public function register_user($data)
{
    //print_r($data);
    //$data=$data[0];
    //print_r($data);
    $params = array( ':email' => $data['email'], ':cf_mobile' => $data['mobile']);
    $sql = "SELECT * FROM phpfox_user, phpfox_user_custom where phpfox_user.email=:email AND phpfox_user_custom.cf_mobile=:cf_mobile";
    $stmt = parent::query($sql, $params, '', 'main');
    while($row = $stmt->fetch(PDO::FETCH_ASSOC)) :
        $res=$row;
    endwhile;

    if(count($res)>=1)
    {
        $result['status']='fail';
        $result['message']='user already exits';
        $result['user_id']=$res['user_id'];
    } else {
        $pass=md5(md5($data['password']).md5('78>'));
        $sql = 'INSERT INTO phpfox_user (full_name, email, password, password_salt, user_group_id, country_iso) 
        VALUES(:full_name, :email, :password, :password_salt, :user_group_id, :country_iso)';

        $params =(array(
            ':full_name' => $data['full_name'],
            ':email' => $data['email'],
            ':password' => $pass,
            ':password_salt' => '"Q>',
            ':user_group_id' => $data['user_group_id'],
            ':country_iso' => $data['location']             
        ));
        //print_array($params);

        parent::query($sql, $params, '', 'main') or die(parent::$dbh->errorInfo());
        # Affected Rows?
        $user_id=parent::$dbh->lastInsertId();

        $sql = 'INSERT INTO phpfox_user_custom (user_id, cf_mobile) 
        VALUES(:user_id, :cf_mobile)';
        $params =(array(
            ':user_id' => $user_id,
            ':cf_mobile' => $data['mobile']
        ));
        //print_array($params);

        parent::query($sql, $params, '', 'main') or die(parent::$dbh->errorInfo());
        # Affected Rows?
        //return parent::$dbh->lastInsertId();
        //return $user_id;
        $result['status']='success';
        $result['message']='Registeration successfully';
        $result['user_id']=$user_id;
    }
    return $result;
}

1 个答案:

答案 0 :(得分:1)

如果两个数据库都驻留在同一个mysql服务器上并且您在没有选择数据库的情况下建立连接,那么只需将数据库的名称放在表名前面,如

INSERT INTO myDBname.myTableName (myKey) VALUES (:myKey);

如果您与一个数据库建立了连接,则可以在插入之前进行更改。

$pdoConnetion->select_db('myDBName');
INSERT INTO myTableName (myKey) VALUES (:myKey);