Jump to content

[PHP, SQL Question]


Mudsaf
 Share

Recommended Posts

Im wondering is it possible to make 2x connections in 1 page whitout getting error from other connection. (Whitout disabling errors on PHP.ini also.) My code so far this.

<?php$local = mysql_connect('localhost',  'root', 'PASSWORD);if  (!$local) {} else {mysql_select_db("DATABASE");	}$server = mysql_connect("HOSTSERVER", "USER", "PASS");if (!$server) {} else {mysql_select_db("DATABASE");}?>

So my point is i want to create php that first tries to connect localhost, if it gets error and cannot connect it tries to connect to "HOSTSERVER". (Whitout getting errors on "Localhost" connection.)

Edited by Mudsaf
Link to comment
Share on other sites

i dont think its possible but then again, it might be possible, have you try something like this

<?phpinclude "mysql_connect.php";$server = mysql_connect("HOSTSERVER", "USER", "PASS");if (!$server) {} else {mysql_select_db("DATABASE");}?>

then you add some select from to see if it works or make a test.php to see if it connects to both connection "thats my opinion on this, might be wrong tho"

Link to comment
Share on other sites

you can make as many conection simultanously as you want but you have to store it somewhere to use it later. you have to mention the connection to use each conection to the functions (mysql function) where conection param is optional. by default those param takes last opened conection. connecting on remote server is dependent on how the remote server has been set up.

Edited by birbal
Link to comment
Share on other sites

<?php  $local = @mysql_connect('localhost', 'root', 'PASSWORD');  if ($local) {    mysql_select_db("DATABASE",$local);  }  else {    $server = @mysql_connect('HOSTSERVER', 'USER', 'PASS');    if ($server) {      mysql_select_db("DATABASE",$server);    }    else {      echo "No connections were able to be established.";    }  }?>

Try that code. It's not tested but it should do what you want. It's important to note that the @ in front of mysql_connect is to suppress errors. So just in case it doesn't connect the it won't give you any errors. Also if you want a certain connection to try to access a database you have to tell it which connection. mysql_select_db() has two arguments, the database name and the connection to use.e.g. mysql_select_db(DATABASE_NAME, MYSQL_CONNECT() )

Edited by Err
Link to comment
Share on other sites

its best to avoid supressing oprator (@). if you dont want to show error in production server make php.ini setting not to display error. instead of that log those error.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...