<?php
####################################################
# Author: Ivan　谭俊青　
# email:drinching@gmail.com
# QQ:55300231
# msn:showsa@live.cn
# copyright: GPL v3.0
# date: 2008-01-15
####################################################

error_reporting(E_ERROR);

set_time_limit(0); # 0表示永远不超时

#####################################################
# 配置
#####################################################
$config = array(
	'host' 		=> '192.168.7.250',
	'username' 	=> 'search',
	'password' 	=> 'xxxxxxxxx',
	'dbname' 	=> '',
	'port' 		=> 3306
);
#####################################################


header( 'Content-type:text/plain; charset=utf-8' );
header( 'Expires: Mon, 08 Aug 2005 08:00:00 GMT' ); // Date in the past
header( 'Cache-Control: no-cache, no-store, must-revalidate, private, max-age=0', false );         // HTTP/1.1
header( 'Pragma: no-cache' );


#####################################################

$ip 	= mysql_escape_string(trim($_REQUEST['ip']));
$port 	= mysql_escape_string(trim($_REQUEST['port']));
$db		= mysql_escape_string(trim($_REQUEST['db']));
$table	= mysql_escape_string(trim($_REQUEST['table']));

if (!($ip and $port and $db and $table))
{
	echo "{ success: false, errors: { reason: '参数传递错误'}}";
}
else
{

	$config['host'] = $ip;
	$config['port'] = $port;
	
	$mysqli_monitor = new mysqli(
		$config['host'],
		$config['username'], 
		$config['password'],
		$config['dbname'],
		$config['port']
	);
	
	# check connection 
	if (mysqli_connect_errno())
	{	
		echo "{ success: false, errors: { reason: ' Connect failed: error no:" . mysqli_connect_errno() . ' error: ' . addslashes( mysqli_connect_error() ) . ' in ' . addslashes( __FILE__ ) . ' on line ' . addslashes( __LINE__ ) . "' }}";
		exit();
	}
	
	$sql = 'select count(1) as count from ' . $db . '.' . $table . ' where 1=1';

	$result = $mysqli_monitor->query($sql);
	if ($result)
	{
		$row = $result->fetch_array(MYSQLI_ASSOC);
	}
	else
	{
		echo "{ success: false, errors: { reason: '请检查数据表是否存在'}}";
		exit();
	}

	echo '{ success: true, count: ' . $row['count'] . '}';

	
	/*
	$count = trim(file_get_contents('http://old.dboa.51.com/get.db.table.record.php?ip='.$ip.'&port='.$port.'&db='.$db.'&table='.$table));
	if($count == '') $count = 'error...';
	
	echo "{ success: true, count: '" . addslashes($count) . "'}";
	*/
	
}