我想知道为什么我这样做有一些错误:
$query = "SELECT * FROM servers ";
while($rows = mysql_fetch_array(mysql_query($query)))
{
$database1 = array();
$database1['host'] = $rows['db_host'];
$database1['user'] = $rows['db_user'];
$database1['password'] = $rows['db_pass'];
$database1['table'] = $rows['db_user'];
$con1 = @mysql_connect($database1['host'], $database1['user'], $database1['password']) or die($connect_error);
if(!$con1) {
exit;
}
$connect_error = 'Sorry, there are some connection problems.';
mysql_select_db($database1['table']) or die($connect_error);
$info = mysql_fetch_assoc(mysql_query("SELECT COUNT(username) as total FROM authme"));
echo "No servidor ".$rows['name']." existem um total de ".$info['total']. " contas registradas, alem disso";
}
确定。以免解释:我有两个不同的数据库,在我编写此查询的fisrt中:
query = "SELECT * FROM servers ";
我在表服务器中有一个mysql连接。所以我必须连接不同的mysql来获取mysql主机,用户和密码。当我得到这些东西时,我必须获得每个连接的值并回显此值当我这样做时,使用COUNT作为变量:
$info = mysql_fetch_assoc(mysql_query("SELECT COUNT(username) as total FROM authme"));
echo "No servidor ".$rows['name']." existem um total de ".$info['total']. " contas registradas, alem disso";
我希望你们不会因为我不会说英语而感到满意):感谢你的关注......
答案 0 :(得分:1)
你应该{@ 1}}一次不在循环中运行。
您的代码会在每个循环中运行mysql_query($query)
。
从以下位置删除:
mysql_query
执行:
while($rows = mysql_fetch_array(mysql_query($query)))
//HERE^
答案 1 :(得分:0)
查看您的脚本,您仍可以进行一些改进。对于初学者,你的变量声明($ connect_error)被创建 AFTER 。
虽然您应该采用mysqli PHP API代替您使用的MySQL函数,但已将其标记为已弃用。
# Where query results are stored
$database1 = array();
# Error Message
$connect_error = 'Sorry, there are some connection problems.';
# mySQL Loop No.1
while($rows = mysql_fetch_array(mysql_query("SELECT (name,db_host,db_user,db_pass) FROM servers"))) {
foreach($rows as $key=>value) {
$database1[$key] = $value;
}
}
# Since this is a duplicate of existing row duplicate
$database1['table'] = $rows['db_user'];
# Connect to new db using details
$con1 = @mysql_connect($database1['host'], $database1['user'], $database1['password']) or die($connect_error);
if(!$con1) {
exit;
}
# Select 2nd DB
mysql_select_db($database1['table']) or die($connect_error);
# mySQL Loop No.2
while($info = mysql_fetch_assoc(mysql_query("SELECT COUNT(username) as total FROM authme"))) {
$total = $info['total'];
}
#Lastly with all data gotten you can show string
echo "No servidor ".$database1['name']." existem um total de ".$total. " contas registradas, alem disso";
请参阅进一步阅读:
http://www.php.net/manual/en/mysqli.quickstart.dual-interface.php