1

我想知道为什么我这样做有一些错误:

 $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";

    }

好的。以免解释:我有两个不同的数据库,在我编写此查询的第一个数据库中:

   query = "SELECT * FROM servers ";

我在表服务器中有一个 mysql 连接。所以我必须连接不同的 mysqls 来获取 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";

我希望你们不理解,因为我的英语说得不太好):谢谢你们的关注..

4

2 回答 2

1

您应该mysql_query($query)不循环运行 1 次。您的代码mysql_query在每个循环中运行。

从...除去:

while($rows = mysql_fetch_array(mysql_query($query)))
                          //HERE^

做:

<?php
$query = "SELECT * FROM servers";
$result = mysql_query($query);

while($rows = mysql_fetch_array($result)){

   // do more stuff
于 2013-08-24T21:31:29.860 回答
0

查看您的脚本,您仍然可以进行一些改进。对于初学者,您的变量声明 ($connect_error) 是在调用之后创建的。

尽管您应该改用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

于 2013-08-24T22:12:07.370 回答