1

I would like to know why am I having some errors doing this:

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

    }

Ok. Lest explain: I have two differents database's, In the fisrt one that I wrote this query:

   query = "SELECT * FROM servers ";

I have in the table servers a mysql connections.So I have to connect in diferrent mysqls to get mysql host, user and password.When I get these things, I have to get a value for each one connection and echo this value using COUNT as a variable, when I did that:

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

I hope that u guys undestand because I dont speak english very well ): and Thank u for your attention..

2
  • Not with your current method Commented Aug 24, 2013 at 21:32
  • Two different mysql databases on the same mysql database server or on different database servers? Commented Aug 24, 2013 at 21:39

2 Answers 2

1

You should run mysql_query($query) 1 time not in loop. Your code makes running mysql_query in every loop.

Remove from:

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

Do:

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

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

   // do more stuff
Sign up to request clarification or add additional context in comments.

Comments

0

Looking at your script there are still some improvements you could make. For starters your variable declaration ($connect_error) is created AFTER it is called.

Though you should adopt the mysqli PHP API instead as the MySQL functions you use have been marked as deprecated.

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

Please see for further reading:

http://www.php.net/manual/en/mysqli.quickstart.dual-interface.php

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.