user3225075
user3225075

Reputation: 373

querying two database in php at a time

Whenever a user submits the registration form I am updating two database at a time. The first database easily gets updated but there is no effect in the second database. Below is my php code

<?php
  $host="localhost";
  $user="aru";
  $password="arus";
  $ur="asus";
  $passord="asus";
  $db="regster";
  $dbn="nsltr";

  $sq=mysql_connect($host,$user,$password) or die ('mysql connecction failed'.mysql_error());

  mysql_select_db($db, $sq) or die('mysql cannot select database'.mysql_error());
  $dbh2 = mysql_pconnect($host, $ur, $passord, true) or die ('mysql connecction failed'.mysql_error());
  mysql_select_db($dbn, $dbh2) or die('mysql cannot select database'.mysql_error());

  if(isset($_POST['btnSubmit1'])){
    $fullName=htmlentities(mysql_real_escape_string($_POST['nme']));
    $emailID=htmlentities(mysql_real_escape_string($_POST['emil']));
    $bc=htmlentities(mysql_real_escape_string($_POST['bch']));
    $bn=htmlentities(mysql_real_escape_string($_POST['bnc']));
    $m=htmlentities(mysql_real_escape_string($_POST['mobe']));
    $em=htmlentities(mysql_real_escape_string($_POST['empy']));
    $a=htmlentities(mysql_real_escape_string($_POST['ofadrs']));
    $me=htmlentities(mysql_real_escape_string($_POST['msge']));
    $doj=date("Y-m-d");
    $sql="insert into rgst values('$emailID', '$fullName', '$bc', '$bn', '$m', '$em', '$a', '$me', '$doj')";
    $rndnm= uniqid();
    $log="insert into nwsletter values('$emailID', '$fullName', '$rndnm')";

    if((!(mysql_query($sql,$sq))) AND (!(mysql_query($log,$dbh2))) ){
      echo '<script language="javascript">alert("Sorry!!! it seems you are already registered");</script>';
    }
    else{
      echo '<script language="javascript">alert("Successfully registered.. Please check your mail address for password and other details");</script>';
    }
  }
?>

By the above code I am able to update register database but there is no change in nsltr database and also no error pops up after executing the codes. Please help, am new to php. Thanks in advance

Upvotes: 0

Views: 72

Answers (2)

asish rauto
asish rauto

Reputation: 637

don't use mysql_ API, instead use mysqli_API. Below is your code using mysqli_ API. Hope it works for you

<?php

$host="localhost";
$user="aru";
$password="arus";
$ur="asus";
$passord="asus";
$db="regster";
$hst="localhost";
    $ur="asus";
$passord="asus";
    $dbn="nsltr";



$sq=mysqli_connect($host,$user,$password,$db);
if (mysqli_connect_errno($sq))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
        if(isset($_POST['btnSubmit1'])){
        $fullName=htmlentities(mysql_real_escape_string($_POST['nme']));
    $emailID=htmlentities(mysql_real_escape_string($_POST['emil']));
    $bc=htmlentities(mysql_real_escape_string($_POST['bch']));
    $bn=htmlentities(mysql_real_escape_string($_POST['bnc']));
    $m=htmlentities(mysql_real_escape_string($_POST['mobe']));
    $em=htmlentities(mysql_real_escape_string($_POST['empy']));
    $a=htmlentities(mysql_real_escape_string($_POST['ofadrs']));
    $me=htmlentities(mysql_real_escape_string($_POST['msge']));
    $doj=date("Y-m-d");
    $sql="insert into regster.rgst values ('$emailID', '$fullName', '$bc', '$bn', '$m', '$em', '$a', '$me', '$doj')";
    $rndnm= uniqid();

    $log="insert into nsltr.nwsletter  values ('$emailID', '$fullName', '$rndnm')";



    if((!(mysqli_query($sq,$sql)))){
        echo '<script language="javascript">alert("Sorry!!! it seems you are already registered");
        </script>';
    }
    else{
    mysqli_close($sq);
    $qq=mysqli_connect($hst,$ur,$passord,$dbn);
if (mysqli_connect_errno($qq))
{
 echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
    else{
    if((!(mysqli_query($qq,$log)))){
        echo "Failed to connect to MySQL: " . mysqli_error($qq);
    }
    else{
            echo '<script language="javascript">alert("Successfully registered.. Please check your mail address for password and other details");
        </script>';
        }
        }

    }
}
?>

Remember, you have to pass all the values for all the fields otherwise you might get "Column count doesn't match value count at row 1" error which will prevent from updating the databases

Upvotes: 0

O. Jones
O. Jones

Reputation: 108651

It looks like your two databases are on the same server, and it looks like your database credentials grant access to both.

In this case you can use the same connection to update both data bases; simply qualify the database names in your SQL.

For example,

insert 
  into regstr.rgst 
values ('$emailID', '$fullName', '$bc', '$bn', '$m', '$em', '$a', '$me', '$doj')

insert 
  into nsltr.nwsletter 
values ('$emailID', '$fullName', '$rndnm')

You can even JOIN tables in different databases as long as they're on the same server and your username has access to both of them.

You should use the _num_rows() call right after running each query, to ensure it actually inserted the number of rows you were expecting.

I'll leave it to somebody else to whine at you about how freakin' dangerous it is to use the obsolete mysql_ API in a production application.

Upvotes: 2

Related Questions