<?
/**
* mysql读写分离
* edit: www.jb200.com
*/
class db
{
public function __construct($sql)
{
$chestr = strtolower(trim($sql));
//判断
sql语句有select关键字的话,就连接读的数据库,否则就连接写数据库
if(substr($chestr,0,6)=='select')
{
echo 'I am using select db..<br>';
$link = mysql_connect("127.0.0.1:3306", "root", "") or die("Could not connect: " . mysql_error());
mysql_select_db("test");
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result, MYSQL_NUM))
{
printf("%s %s", $row[0],$row[1]);
}
echo mysql_get_host_info($link).mysql_get_server_info($link).mysql_get_proto_info($link).mysql_get_client_info().'<br>';
}
else
{
echo 'I am using insert db..<br>';
$link = mysql_connect("127.0.0.2:3306","root","") or die("Could not connect: " . mysql_error());
mysql_select_db("test");
$result = mysql_query($sql);
echo @mysql_affected_rows($result);
echo mysql_get_host_info($link).mysql_get_server_info($link).mysql_get_proto_info($link).mysql_get_client_info().'<br>';
}
}
}
$d = new db(" update `users` set `select`='fasdf' where `id` =1");
$d2 = new db(" SELECT * from `users`");