Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

hosting and php

Status
Not open for further replies.

csphard

Programmer
Apr 5, 2002
194
US
I started using a company to host sample sites I have put together using php so I would have a portfolio. What I notice is how long it takes to run the php scripts I created. Even when it is simple such as the following.
Why?
link
<?php include "include/dbstore.php"; ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "<html xmlns="<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>test</title>
</head>

<body>
<table width="100%" border="1" cellspacing="0" bordercolor="#000000">
<thead><tr>
<th bgcolor="#000000"><span class="style1">Address</span></th>
<th bgcolor="#000000"><span class="style1">City</span></th>
<th bgcolor="#000000"><span class="style1">TG</span></th>
</tr>

<?php

$sql = "select type,color,price from products";
$result = mysql_query($sql);

while ($row = mysql_fetch_array($result)) {
/*echo '<tr><td><a href="location_geocode.php?address=' . $row['address'] . ',' . ucwords($row['city']) . ',CA">' . $row['address'] . '</td>';*/
echo '<tr><td>' . $row['type'] . '</td>';
echo ' <td>' . $row['color'] . '</td>';
echo ' <td>' . $row['price'] . '</td>';
}

?></table>
<!-- this is container end -->
</body>
</html>


dbstore.php
<?php

mysql_connect("host","userid","password") or die(mysql_error());
mysql_select_db("db") or die(mysql_error());


?>
howard
 
Its hard to say, but my guess it has something to do with the load or hardware capacity of your hosting company. Is the mysql server on the same machine as your php server? Do php pages that don't have any database calls load slowly?

-----------------------------------------
I cannot be bought. Find leasing information at
 
the colour fuchsia is spelt as i typed. not fuschia. common mistake.

as for the question of speed, can you shove up a phpinfo script on your server and post the link? that may give us some more info. also you could ask your host what equipment they are using.

should you wish, i can put your code and database on a few of my servers and post you some rough load times.

lastly, note that html tables are very slow to load as a browser needs to have all the table in its memory before it can render it. whilst this won't explain every bit of slowness, you might see what you get with this script. it will also give some ideas of actual script execution time.

Code:
<?php 
class timer{
	/**
	 * method to start the timer
	 * @return 
	 */
	public function start(){
		$this->start = microtime(true);
	}
 
	/**
	 * method to pause the timer
	 * @return 
	 */
	private function stop($event){
		$this->stop[] = array($event, microtime(true));
	}
 
	/**
	 * method to stop the timer and report on the amount of time taken
	 * @return 
	 * @param object $format[optional]
	 */
	public function report(){
		$this->stop();
		echo "<pre>";
		foreach ($this->stop as $key=>$e){
			echo "$key \t " . number_format($this->diff($e[1]), 8) . "\t" . $e[0] . "\r\n";
		}
		echo "</pre>";
	}
 
	/**
	 * calculate the amount of time taken.
	 */
	private function diff(){
		$this->diff = $this->stop - $this->start;
	}
}
?>
<?php include "include/dbstore.php"; ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">[/URL]
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>test</title>
<style type="text/css">
	html, body, div, img {
		margin: 0; 
		padding:0;
		border: none;
	}
	#tableWrapper{
		width:80%;
		overflow-y:hidden;
		margin:0 auto;	
	}
	.tableRow{
		clear:left;
		overflow-y:hidden;
		min-width: 500px;
	}
	.tableColumn{
		float:left;
		padding: 2px;
		border: solid 1px black;
	}
	.type{
		width: 40%;
		text-align:left;
	}
	.color{
		width:40%;
		text-align:center;
	}
	.price{
		width: 20%;
		text-align:right;
	}
</style>
</head>

<body>
<div id="tablewrapper">
	<div id="tableHeader" class="tableRow header">
		<div class="tableColumn type">Type</div>
		<div class="tableColumn color">Color</div>
		<div class="tableColumn price">Price</div>
	</div>
<?php
$timer = new timer();
$sql = "select type,color,price from products";
$result = mysql_query($sql);
$timer->stop('Query performed');
$c=1;
if ($result){
	$timer->stop('starting to loop through records');
	while ($row=mysql_fetch_assoc($result)){
		echo <<<HTML
		
	<div class="tableRow header">
		<div class="tableColumn type">{$row['type']}</div>
		<div class="tableColumn color">{$row['color']}</div>
		<div class="tableColumn price">{$row['price']}</div>
	</div>
	
HTML;
		$timer->stop ('Output record ' . $c );
		$c++;
	}
	$timer->stop('finished outputting records');
}
?>
</div>  
<?php $timer->report(); ?>
</body>
</html>
 
thanks I will try that this afternoon
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top