Jump to content

[PHP, SQL Question]


Mudsaf

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.)

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.

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() )

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...