doumi9618 2013-06-04 17:58
浏览 72

如何检测某个IP目前是否与PHP和MySQL连接?

Question: How can I put 2 or more database connection in one PHP file with a case that one or more servers are not connecting to the network?

The Story Line Sorry if this question is already existed, but I already searched it and still I don't quite understand..

so, I'm trying to make a simple distributed database system using MySQL, and I'm not using any complicated stuffs since I'm a complete beginner... I use a standard connection on MySQL (mysql_connect)...in I put it as an included file... because it's a distributed database, in which in one website there'll be more than one connection attemp to make, if one of the connection failed...

like this, I made two conn file..it's just examples though...

<?php
$server="192.168.0.3";
$user="user";
$pass="pass";
$db="db_1";

$conn = mysql_connect($server,$user,$pass);
if(!$conn) die "connection error";
$sel_db = mysql_select_db($db,$conn);
if(!sel_db) die "wrong db";
?>

so I made the second one with db_2 as the database... and I put the on the website using if..

<?php
require_once "conn_1.php";
if (empty($conn)) {
   require_once "conn_2.php";
}
?>

and I intentionally close/cut the connection on the first server...so if the first connect attemp failed, it will goes to the second one.. but it ended up showing error "trying to connect to a unexisted host", well I forgot about the detail but something like that...and didn't show anything else..

so I tried to close the second server... I thought the result will be different, it has to be showing a normal result since the first connection didn't fail...but the result is, the website still showed a same warning error..

so I just thought that instead of using those connection files, I have to put an IP tracking code to check the other server is online or not, and then called the connection file...

or is it another way to do it...?

Sorry if you don't understand my question, please tell me if my question is a bit confusing.. and thanks..

  • 写回答

1条回答 默认 最新

  • dougua4836 2013-06-04 18:06
    关注

    Why are your servers down ? Ideally if you have multiple MySQL databases you will have multiple database servers with some sort of synchronize between the databases.

    Each webserver should have 1 database server. and then the datbase servers should all be updated by 1 master database server.

    Answering your question more directly

    $conn = mysql_connect($server,$user,$pass);
    if(!$conn) {
    
    // NOW try and connect to somewhere else
    $conn = mysql_connect($server2,$user,$pass);
    }
    

    Also dont use mysql_* functions any more - look into mysqli_ (http://php.net/manual/en/book.mysqli.php) or pdo (http://php.net/manual/en/book.pdo.php)

    评论

报告相同问题?

悬赏问题

  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作