?
Solved

second part of my script wont run....

Posted on 2008-11-04
14
Medium Priority
?
215 Views
Last Modified: 2013-12-13
i'm lost here, I know that i can get data from both databases, i've commented out the both "half's" of the script just to see if my $display_block actually works, and it does...i get data from either database when i'm just pulling one at a time.

But when try to get data at the same time in one pass....i only get data from 'NYTD' and not from my "seadb" connection...

is this a flow issue? its seem like the second half of the script is never being parsed... I know this because i added just a generic ' echo "php parsed the second half" towards the bottom of the script...and it doesnt display
<?php
# header("Content-Type: text/xml");
#echo "<stats> \n";
 
 
$bg_test = 2;
 
$hostname_nydb = "localhost";
$database_nydb = "nytd";
$username_nydb = "root";
$password_nydb = "";
$nytddbcon = mysqli_connect("$hostname_nydb", "$username_nydb", "$password_nydb", "$database_nydb");
$hostname_seadb = "sysdb1";
$database_seadb = "nytd";
$username_seadb = "stats";
$password_seadb = "stats";
$seadbcon = mysqli_connect("$hostname_seadb", "$username_seadb", "$password_seadb", "$database_seadb");
 
if (mysqli_connect_errno()) {
        printf("Connect failed: %s\n", mysqli_connect_error());
        exit();
} else {
        $SERVERS = file('lists/wwwgslb.list');
        foreach($SERVERS as $SERVER) {
        $SRV = rtrim($SERVER);
#       echo $SRV;
 
        $nytdsql = "SELECT * FROM stats_page_all where server_name = '$SRV'";
 
        $seasql = "SELECT * FROM stats_page_all where server_name = '$SRV'";
        #$sql = "SELECT * FROM stats_page where server_name = 'web%t'";
#       echo $sql;
        #$sql = "SELECT * FROM host";
                mysql_select_db($database_nydb);
        $nytdres = mysqli_query($nytddbcon, $nytdsql);
 
        if ($nytdres) {
                while ($newArray = mysqli_fetch_array($nytdres, MYSQLI_ASSOC)) {
                  $id  = $newArray['id'];
                  $hostname = $newArray['description'];
                  $load1 = $newArray['load1'];
                  $cpu_idle = $newArray['cpu_idle'];
                  $connections = $newArray['connections'];
                  $hostname = $newArray['description'];
                  $no_hits = $newArray['hits'];
                  $diff_hits = $newArray['diff_hits'];
                  $hitsper = $newArray['hitsper'];
                  $hitspersec = sprintf("%.0f", $hitsper);
                  $dl_time = $newArray['dl_time'];
                  $date = $newArray['date'];
                  $swap_avail = $newArray['swap_avail'];
                  $swap_total = $newArray['swap_total'];
                  $result_swap =  sprintf("%.2f", $swap_avail / $swap_total * 100);
                  #$result_swap =  ( $swap_avail / $swap_total * 100);
                  $real_avail = $newArray['real_avail'];
                  $real_total = $newArray['real_total'];
                  $result_real = sprintf("%.0f", $real_avail / $real_total * 100);
if (($bg_test % 2) == 0){
        $BGCOLOR = "FFFFFF";
        }
else {
        $BGCOLOR = "cccccc";
        }
 
$bg_test++;
 
echo $display_block =
"
    <server>
        <id>$id</id>
        <name>$hostname</name>
        <load>$load1</load>
        <cpuidle>$cpu_idle</cpuidle>
        <memfree>$result_real</memfree>
        <swapfree>$result_swap</swapfree>
        <numconns>$connections</numconns>
        <hitspersec>$hitspersec</hitspersec>
        <numhits>$no_hits</numhits>
        <dltime>$dl_time</dltime>
        <lastupdated>$date</lastupdated>
    </server>
 
"; 
}
       } else {
              printf("Could not retrieve records: %s\n", mysqli_error($nytddbcon));
       }
}
##--------------------------------------------------------------------------------
## start of the seatle pull/display
##------------------------------------------------------------------------------
           mysql_select_db($database_seadb);
       $seares = mysqli_query($seadbcon, $seasql);
 
        if ($seares) {
                while ($newArray1 = mysqli_fetch_array($seares, MYSQLI_ASSOC)) {
                  $id  = $newArray1['id'];
                  $hostname = $newArray1['description'];
                  $load1 = $newArray1['load1'];
                  $cpu_idle = $newArray1['cpu_idle'];
                  $connections = $newArray1['connections'];
                  $hostname = $newArray1['description'];
                  $no_hits = $newArray1['hits'];
                  $diff_hits = $newArray1['diff_hits'];
                  $hitsper = $newArray1['hitsper'];
                  $hitspersec = sprintf("%.0f", $hitsper);
                  $dl_time = $newArray1['dl_time'];
                  $date = $newArray1['date'];
                  $swap_avail = $newArray1['swap_avail'];
                  $swap_total = $newArray1['swap_total'];
                  $result_swap =  sprintf("%.2f", $swap_avail / $swap_total * 100);
                  #$result_swap =  ( $swap_avail / $swap_total * 100);
                  $real_avail = $newArray1['real_avail'];
                  $real_total = $newArray1['real_total'];
                  $result_real = sprintf("%.0f", $real_avail / $real_total * 100);
if (($bg_test % 2) == 0){
        $BGCOLOR = "FFFFFF";
        }
else {
        $BGCOLOR = "cccccc";
        }
 
$bg_test++;
 
echo $display_block =
"
    <server>
        <id>$id</id>
        <name>$hostname</name>
        <load>$load1</load>
        <cpuidle>$cpu_idle</cpuidle>
        <memfree>$result_real</memfree>
        <swapfree>$result_swap</swapfree>
        <numconns>$connections</numconns>
        <hitspersec>$hitspersec</hitspersec>
        <numhits>$no_hits</numhits>
        <dltime>$dl_time</dltime>
        <lastupdated>$date</lastupdated>
    </server>
 
"; 
}
       } else {
               printf("Could not retrieve records: %s\n", mysqli_error($seadbcon));
       }
}
 
       mysqli_free_result($nytdres);
                mysqli_free_result($seares);
        mysqli_close($nytddbcon);
                mysqli_close($seadbcon);
 
echo "</farm>";
#echo "</stats>";
?>

Open in new window

0
Comment
Question by:jedblack
  • 6
  • 4
  • 3
  • +1
14 Comments
 
LVL 8

Expert Comment

by:CoyotesIT
ID: 22880364
I would try moving your

$seadbcon = mysqli_connect("$hostname_seadb", "$username_seadb", "$password_seadb", "$database_seadb");

down when you are attempting to use the connection. The issue may be that the connection ID for each instance is the same, therefore only one is actually open.

When you are finished with your $nytddbon connection close it, then open the other database.

Does this have any impact?

~CoyotesIT
0
 
LVL 82

Expert Comment

by:hielo
ID: 22880381
>>mysql_select_db($database_seadb);
did you mean mysqli_select_db?
0
 
LVL 1

Author Comment

by:jedblack
ID: 22880400
CoyotesIT,

I tried your suggestion.  Unfortunatly, it did'nt help.....
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 1

Author Comment

by:jedblack
ID: 22880441
Heilo,

I tried both mysqli_select_db and mysql_select_db...

both work...but only for the first part of the script... i still see no data comming from my Seatle connection
0
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 22880566
In line 19 you are checking for a connect error for the connect command in line 17.
You should also check for connect errors after line 12:

$nytddbcon = mysqli_connect("$hostname_nydb", "$username_nydb", "$password_nydb", "$database_nydb");
if(!$nytddbcon) {
  printf("Connect failed: %s\n", mysqli_connect_error());
  exit();
}

In lines 34 and 92 you use mysql_selsect_db(), you should use mysqli_select_db().
As a consequence, you are changing the database on the wrong server... Use this:

mysqli_select_db($nytddbcon,$database_nydb);
mysqli_select_db($seadbcon,$database_seadb);

These functions should be called BEFORE the loop, not within.
0
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 22880606
mysql_selsect_db() changes the database on the LAST CONNECTED server...
0
 
LVL 82

Expert Comment

by:hielo
ID: 22880617
you need to iterate over the $SERVERS again:
<?php
# header("Content-Type: text/xml");
#echo "<stats> \n";
echo "<farm>\n";
$bg_test = 2;
 
$hostname_nydb = "localhost";
$database_nydb = "nytd";
$username_nydb = "root";
$password_nydb = "";
$nytddbcon = mysqli_connect("$hostname_nydb", "$username_nydb", "$password_nydb", "$database_nydb") or die("Connect failed: ", mysqli_connect_error());
 
$hostname_seadb = "sysdb1";
$database_seadb = "nytd";
$username_seadb = "stats";
$password_seadb = "stats";
$seadbcon = mysqli_connect("$hostname_seadb", "$username_seadb", "$password_seadb", "$database_seadb") or die("Connect failed: ", mysqli_connect_error());
 
 
$SERVERS = file('lists/wwwgslb.list');
foreach($SERVERS as $SERVER)
{
	$SRV = rtrim($SERVER);
	#echo $SRV;
 
	$nytdsql = "SELECT * FROM stats_page_all where server_name = '$SRV'";
 
	#$sql = "SELECT * FROM stats_page where server_name = 'web%t'";
	#echo $sql;
	#$sql = "SELECT * FROM host";
	mysqli_select_db($database_nydb);
	$nytdres = mysqli_query($nytddbcon, $nytdsql);
 
	if ($nytdres)
	{
		while ($newArray = mysqli_fetch_array($nytdres, MYSQLI_ASSOC))
		{
			$id  = $newArray['id'];
			$hostname = $newArray['description'];
			$load1 = $newArray['load1'];
			$cpu_idle = $newArray['cpu_idle'];
			$connections = $newArray['connections'];
			$hostname = $newArray['description'];
			$no_hits = $newArray['hits'];
			$diff_hits = $newArray['diff_hits'];
			$hitsper = $newArray['hitsper'];
			$hitspersec = sprintf("%.0f", $hitsper);
			$dl_time = $newArray['dl_time'];
			$date = $newArray['date'];
			$swap_avail = $newArray['swap_avail'];
			$swap_total = $newArray['swap_total'];
			$result_swap =  sprintf("%.2f", $swap_avail / $swap_total * 100);
			#$result_swap =  ( $swap_avail / $swap_total * 100);
			$real_avail = $newArray['real_avail'];
			$real_total = $newArray['real_total'];
			$result_real = sprintf("%.0f", $real_avail / $real_total * 100);
			if (($bg_test % 2) == 0)
			{
				$BGCOLOR = "FFFFFF";
			}
			else
			{
				$BGCOLOR = "cccccc";
			}
			$bg_test++;
 
			echo $display_block ="
                   <server>
                       <id>$id</id>
                       <name>$hostname</name>
                       <load>$load1</load>
                       <cpuidle>$cpu_idle</cpuidle>
                       <memfree>$result_real</memfree>
                       <swapfree>$result_swap</swapfree>
                       <numconns>$connections</numconns>
                       <hitspersec>$hitspersec</hitspersec>
                       <numhits>$no_hits</numhits>
                       <dltime>$dl_time</dltime>
                       <lastupdated>$date</lastupdated>
                   </server>";
		}
	}
	else
	{
		printf("Could not retrieve records: %s\n", mysqli_error($nytddbcon));
	}
}
 
foreach($SERVERS as $SERVER){
	$seasql = "SELECT * FROM stats_page_all where server_name = '$SRV'";
 
##--------------------------------------------------------------------------------
## start of the seatle pull/display
##------------------------------------------------------------------------------
	mysqli_select_db($database_seadb);
	$seares = mysqli_query($seadbcon, $seasql);
 
	if ($seares)
	{
                while ($newArray1 = mysqli_fetch_array($seares, MYSQLI_ASSOC)) {
                  $id  = $newArray1['id'];
                  $hostname = $newArray1['description'];
                  $load1 = $newArray1['load1'];
                  $cpu_idle = $newArray1['cpu_idle'];
                  $connections = $newArray1['connections'];
                  $hostname = $newArray1['description'];
                  $no_hits = $newArray1['hits'];
                  $diff_hits = $newArray1['diff_hits'];
                  $hitsper = $newArray1['hitsper'];
                  $hitspersec = sprintf("%.0f", $hitsper);
                  $dl_time = $newArray1['dl_time'];
                  $date = $newArray1['date'];
                  $swap_avail = $newArray1['swap_avail'];
                  $swap_total = $newArray1['swap_total'];
                  $result_swap =  sprintf("%.2f", $swap_avail / $swap_total * 100);
                  #$result_swap =  ( $swap_avail / $swap_total * 100);
                  $real_avail = $newArray1['real_avail'];
                  $real_total = $newArray1['real_total'];
                  $result_real = sprintf("%.0f", $real_avail / $real_total * 100);
if (($bg_test % 2) == 0){
        $BGCOLOR = "FFFFFF";
        }
else {
        $BGCOLOR = "cccccc";
        }
 
$bg_test++;
 
echo $display_block =
"
    <server>
        <id>$id</id>
        <name>$hostname</name>
        <load>$load1</load>
        <cpuidle>$cpu_idle</cpuidle>
        <memfree>$result_real</memfree>
        <swapfree>$result_swap</swapfree>
        <numconns>$connections</numconns>
        <hitspersec>$hitspersec</hitspersec>
        <numhits>$no_hits</numhits>
        <dltime>$dl_time</dltime>
        <lastupdated>$date</lastupdated>
    </server>
 
"; 
}
       } else {
               printf("Could not retrieve records: %s\n", mysqli_error($seadbcon));
       }
}
}
       mysqli_free_result($nytdres);
                mysqli_free_result($seares);
        mysqli_close($nytddbcon);
                mysqli_close($seadbcon);
 
echo "</farm>";
#echo "</stats>";
?>
 

Open in new window

0
 
LVL 82

Expert Comment

by:hielo
ID: 22880670
typos found on the previous post. Try this:
<?php
# header("Content-Type: text/xml");
#echo "<stats> \n";
echo "<farm>\n";
$bg_test = 2;
 
$hostname_nydb = "localhost";
$database_nydb = "nytd";
$username_nydb = "root";
$password_nydb = "";
$nytddbcon = mysqli_connect("$hostname_nydb", "$username_nydb", "$password_nydb", "$database_nydb") or die("Connect failed: ", mysqli_connect_error());
 
$hostname_seadb = "sysdb1";
$database_seadb = "nytd";
$username_seadb = "stats";
$password_seadb = "stats";
$seadbcon = mysqli_connect("$hostname_seadb", "$username_seadb", "$password_seadb", "$database_seadb") or die("Connect failed: ", mysqli_connect_error());
 
 
$SERVERS = file('lists/wwwgslb.list');
foreach($SERVERS as $SERVER)
{
	$SRV = rtrim($SERVER);
	#echo $SRV;
 
	$nytdsql = "SELECT * FROM stats_page_all where server_name = '$SRV'";
 
	#$sql = "SELECT * FROM stats_page where server_name = 'web%t'";
	#echo $sql;
	#$sql = "SELECT * FROM host";
	mysqli_select_db($database_nydb);
	$nytdres = mysqli_query($nytddbcon, $nytdsql);
 
	if ($nytdres)
	{
		while ($newArray = mysqli_fetch_array($nytdres, MYSQLI_ASSOC))
		{
			$id  = $newArray['id'];
			$hostname = $newArray['description'];
			$load1 = $newArray['load1'];
			$cpu_idle = $newArray['cpu_idle'];
			$connections = $newArray['connections'];
			$hostname = $newArray['description'];
			$no_hits = $newArray['hits'];
			$diff_hits = $newArray['diff_hits'];
			$hitsper = $newArray['hitsper'];
			$hitspersec = sprintf("%.0f", $hitsper);
			$dl_time = $newArray['dl_time'];
			$date = $newArray['date'];
			$swap_avail = $newArray['swap_avail'];
			$swap_total = $newArray['swap_total'];
			$result_swap =  sprintf("%.2f", $swap_avail / $swap_total * 100);
			#$result_swap =  ( $swap_avail / $swap_total * 100);
			$real_avail = $newArray['real_avail'];
			$real_total = $newArray['real_total'];
			$result_real = sprintf("%.0f", $real_avail / $real_total * 100);
			if (($bg_test % 2) == 0)
			{
				$BGCOLOR = "FFFFFF";
			}
			else
			{
				$BGCOLOR = "cccccc";
			}
			$bg_test++;
 
			echo $display_block ="
                   <server>
                       <id>$id</id>
                       <name>$hostname</name>
                       <load>$load1</load>
                       <cpuidle>$cpu_idle</cpuidle>
                       <memfree>$result_real</memfree>
                       <swapfree>$result_swap</swapfree>
                       <numconns>$connections</numconns>
                       <hitspersec>$hitspersec</hitspersec>
                       <numhits>$no_hits</numhits>
                       <dltime>$dl_time</dltime>
                       <lastupdated>$date</lastupdated>
                   </server>";
		}
	}
	else
	{
		printf("Could not retrieve records: %s\n", mysqli_error($nytddbcon));
	}
}
 
 
##------------------------------------------------------------------------------
## start of the seatle pull/display
##------------------------------------------------------------------------------
foreach($SERVERS as $SERVER)
{
	$SRV = rtrim($SERVER);
	#echo $SRV;
 
	$seasql = "SELECT * FROM stats_page_all where server_name = '$SRV'";
 
	mysqli_select_db($database_seadb);
	$seares = mysqli_query($seadbcon, $seasql);
 
	if ($seares)
	{
		while ($newArray1 = mysqli_fetch_array($seares, MYSQLI_ASSOC))
		{
			$id  = $newArray1['id'];
			$hostname = $newArray1['description'];
			$load1 = $newArray1['load1'];
			$cpu_idle = $newArray1['cpu_idle'];
			$connections = $newArray1['connections'];
			$hostname = $newArray1['description'];
			$no_hits = $newArray1['hits'];
			$diff_hits = $newArray1['diff_hits'];
			$hitsper = $newArray1['hitsper'];
			$hitspersec = sprintf("%.0f", $hitsper);
			$dl_time = $newArray1['dl_time'];
			$date = $newArray1['date'];
			$swap_avail = $newArray1['swap_avail'];
			$swap_total = $newArray1['swap_total'];
			$result_swap =  sprintf("%.2f", $swap_avail / $swap_total * 100);
			#$result_swap =  ( $swap_avail / $swap_total * 100);
			$real_avail = $newArray1['real_avail'];
			$real_total = $newArray1['real_total'];
			$result_real = sprintf("%.0f", $real_avail / $real_total * 100);
			if (($bg_test % 2) == 0)
			{
				$BGCOLOR = "FFFFFF";
			}
			else
			{
				$BGCOLOR = "cccccc";
			}
			$bg_test++;
			echo $display_block =
			"
              <server>
                  <id>$id</id>
                  <name>$hostname</name>
                  <load>$load1</load>
                  <cpuidle>$cpu_idle</cpuidle>
                  <memfree>$result_real</memfree>
                  <swapfree>$result_swap</swapfree>
                  <numconns>$connections</numconns>
                  <hitspersec>$hitspersec</hitspersec>
                  <numhits>$no_hits</numhits>
                  <dltime>$dl_time</dltime>
                  <lastupdated>$date</lastupdated>
              </server>
			"; 
		}//end:while
	}
	else
	{
		printf("Could not retrieve records: %s\n", mysqli_error($seadbcon));
	}
}//end:foreach
 
 
mysqli_free_result($nytdres);
mysqli_free_result($seares);
mysqli_close($nytddbcon);
mysqli_close($seadbcon);
 
echo "</farm>";
#echo "</stats>";
?>
 

Open in new window

0
 
LVL 1

Author Comment

by:jedblack
ID: 22880982
heilo,

I tried your last post, nothing is displaying now through my browser

I get this when i run through the command line...

PHP Parse error:  parse error, unexpected ',' in /web/stats/wwwtest.php on line 11

When remove the comma, I get this

PHP Parse error:  parse error, unexpected T_STRING in /web/stats/wwwtest.php on line 11


0
 
LVL 82

Expert Comment

by:hielo
ID: 22881034
>>or die("Connect failed: ". mysqli_connect_error());

change the comma to a period:
...or die("Connect failed: " . mysqli_connect_error());

0
 
LVL 82

Accepted Solution

by:
hielo earned 2000 total points
ID: 22881039

$hostname_nydb = "localhost";
$database_nydb = "nytd";
$username_nydb = "root";
$password_nydb = "";
$nytddbcon = mysqli_connect("$hostname_nydb", "$username_nydb", "$password_nydb", "$database_nydb") or die("Connect failed: ". mysqli_connect_error());
 
$hostname_seadb = "sysdb1";
$database_seadb = "nytd";
$username_seadb = "stats";
$password_seadb = "stats";
$seadbcon = mysqli_connect("$hostname_seadb", "$username_seadb", "$password_seadb", "$database_seadb") or die("Connect failed: ". mysqli_connect_error());

Open in new window

0
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 22881094
mysqli_select_db() needs the link as the first parameter:


mysqli_select_db($nytddbcon,$database_nydb);
mysqli_select_db($seadbcon,$database_seadb);

These functions should be called BEFORE the loop, not within.
0
 
LVL 1

Author Closing Comment

by:jedblack
ID: 31513270
Hielo, thanks so much, that worked....
0
 
LVL 82

Expert Comment

by:hielo
ID: 22881355
you are welcome.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to create an extensible mechanism for linked drop downs.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses
Course of the Month14 days, 14 hours left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question