Click to See Complete Forum and Search --> : PHP and XML


ForeverIrise
12-29-2006, 07:41 PM
Can I execute a PHP while script that reads data from a MySQL database to populate an XML file?

If so, how would I go about starting this.

If not, what is another option that does something similar.

Thank you for your time.
-Cesar

NogDog
12-29-2006, 08:38 PM
Yes, you can. Here's a small example of grabbing a list of names from a DB and outputting it as XML:

<?php
$connx = mysql_connect('localhost', 'username', 'password') or die("connection error");
mysql_select_db('database_name') or die(mysql_error());
$sql = "SELECT lastname, firstname FROM tablename ORDER BY lastname, firstname";
$result = mysql_query($sql) or die(mysql_error()."<br />query: $sql");
header("Content-Type: text/xml");
echo "<?xml version='1.0' encoding='UTF-8'?>\n";
echo "<names>\n";
while($row = mysql_fetch_assoc($result))
{
echo "<name>\n";
echo "<lastname>{$row['lastname']}</lastname>\n";
echo "<firstname>{$row['firstname']}</firstname>\n";
echo "</name>\n";
}
echo "</names>";

ForeverIrise
12-29-2006, 09:58 PM
So the actual file wouldn't be an .xml extension but a .php.

That was one of the doubts I had, I makes sense that it would be .php but I just wasn't sure. Thanks for the example by the way. I appreciate it.

NogDog
12-29-2006, 10:10 PM
It could be .xml if you configure your webserver to process .xml files through the PHP parser. (For instance, on Apache webservers, you can use a .htaccess file within a given directory to specify that for all such files in that directory and its sub-directories.)

ForeverIrise
12-29-2006, 11:12 PM
echo "<lastname>{$row['lastname']}</lastname>\n";



Do I have to do it that way or can I do it this way:


echo "<lastname>" . $row['lastname'] . "</lastname>\n";


Are they pretty much the same thing or do they pass the variable differently?

NogDog
12-29-2006, 11:27 PM
They're functionally equivalent. It's mainly a matter of taste. Possibly one way might save you a micro-second or two of processing time over the other, but probably not enough that you'd ever have to worry about it.

NightShift58
12-29-2006, 11:37 PM
That's a lingering question/doubt I've had...

And, not being sure, in one application I'll go for the {} variety - which I prefer - and in the next one, it'll be dot-concatenation.

Realizing that we're talking about minute differences between the two, I don't know which one would be faster.

The variable has to be resolved either way. The issue, I guess, is which of the two methods is able to get PHP's attention more efficiently. I could raise theoretical arguments either way, but would fail to convince even myself...

So I pray tell me, NogDog: Which one is faster?

NogDog
12-30-2006, 12:00 AM
Here's a test script I just tried:

<?php
$times = array();
// initialize use of microtime
$times['init'] = microtime(TRUE);
header("Content-Type: text/plain");
$test['sample'] = 'test';
for($test=1; $test<11; $test++)
{
ob_start();
$times[1] = microtime(TRUE);
for($i=1; $i<=1000; $i++)
{
echo 'This is a ' . $test['sample'] . '
';
}
$times[2] = microtime(TRUE);
for($i=1; $i<=1000; $i++)
{
echo "This is a " . $test['sample'] . "\n";
}
$times[3] = microtime(TRUE);
for($i=1; $i<=1000; $i++)
{
echo "This is a {$test['sample']}\n";
}
$times[4] = microtime(TRUE);
ob_end_clean();
echo "\nTEST $test:\n";
printf("Single quotes with concatenation: %f seconds\n", $times[2] - $times[1]);
printf("Double quotes with concatenation: %f seconds\n", $times[3] - $times[2]);
printf("Double quotes with interpolation: %f seconds\n", $times[4] - $times[3]);
}
?>

And here is a typical result, seeming to indicate virtually no difference between single and double quotes with concatenation, while the double quotes with {$var} interpolation taking about twice as long.

TEST 1:
Single quotes with concatenation: 0.006703 seconds
Double quotes with concatenation: 0.003526 seconds
Double quotes with interpolation: 0.029191 seconds

TEST 2:
Single quotes with concatenation: 0.003397 seconds
Double quotes with concatenation: 0.003333 seconds
Double quotes with interpolation: 0.007217 seconds

TEST 3:
Single quotes with concatenation: 0.003670 seconds
Double quotes with concatenation: 0.003375 seconds
Double quotes with interpolation: 0.029002 seconds

TEST 4:
Single quotes with concatenation: 0.003805 seconds
Double quotes with concatenation: 0.013018 seconds
Double quotes with interpolation: 0.006931 seconds

TEST 5:
Single quotes with concatenation: 0.003527 seconds
Double quotes with concatenation: 0.003199 seconds
Double quotes with interpolation: 0.007357 seconds

TEST 6:
Single quotes with concatenation: 0.003162 seconds
Double quotes with concatenation: 0.003295 seconds
Double quotes with interpolation: 0.007064 seconds

TEST 7:
Single quotes with concatenation: 0.003540 seconds
Double quotes with concatenation: 0.003198 seconds
Double quotes with interpolation: 0.007354 seconds

TEST 8:
Single quotes with concatenation: 0.003484 seconds
Double quotes with concatenation: 0.003208 seconds
Double quotes with interpolation: 0.028859 seconds

TEST 9:
Single quotes with concatenation: 0.003308 seconds
Double quotes with concatenation: 0.003682 seconds
Double quotes with interpolation: 0.006683 seconds

TEST 10:
Single quotes with concatenation: 0.003418 seconds
Double quotes with concatenation: 0.003185 seconds
Double quotes with interpolation: 0.007231 seconds

NightShift58
12-30-2006, 12:12 AM
The performance difference is not that great in real terms but significant as a percentage. A factor to consider in long-running batch updates...

Also, the {} seems to go completely out of whack every so often. Strange...

Thanks for taking the time to script this.

NightShift58
12-30-2006, 04:43 PM
I took your basic script from yesterday and used it to test different types of concatenation and interpolation.<?php
$times = array();
// initialize use of microtime
$times['init'] = microtime(TRUE);
header("Content-Type: text/plain");
$test['sample'] = 'test';
for($test=1; $test<11; $test++)
{
ob_start();
$times[1] = microtime(TRUE);
for($i=1; $i<=1000; $i++)
{
echo 'This is a ' . $test['sample'] . '\n';
}
$times[2] = microtime(TRUE);
for($i=1; $i<=1000; $i++)
{
echo 'This is a ' . $test[sample] . '\n';
}
$times[3] = microtime(TRUE);
for($i=1; $i<=1000; $i++)
{
echo "This is a {$test['sample']}" . "\n";
}
$times[4] = microtime(TRUE);
for($i=1; $i<=1000; $i++)
{
echo "This is a " . "{$test['sample']}" . "\n";
}
$times[5] = microtime(TRUE);
for($i=1; $i<=1000; $i++)
{
echo "This is a {$test[sample]}" . "\n";
}
$times[6] = microtime(TRUE);
ob_end_clean();
echo "\nTEST $test:\n";
printf("Case 01: %f seconds\n", $times[2] - $times[1]);
printf("Case 02: %f seconds\n", $times[3] - $times[2]);
printf("Case 03: %f seconds\n", $times[4] - $times[3]);
printf("Case 04: %f seconds\n", $times[5] - $times[4]);
printf("Case 05: %f seconds\n", $times[6] - $times[5]);
}
?>The results of "concatenated interpolation" (Case 04) , as opposed to the simpler "interpolation" (Case 03), surprises me and I don't know what to make of it.

Also interesting is that although using array keys without quotes will work, it takes much, much longer to resolve without than with the quotes (Case 02 and 05). Using single quotes or double quotes for array keys seems to have no bearing on performance, as you demonstrated previously

NogDog
12-30-2006, 04:56 PM
An unquoted, non-integer array key that is not yet a defined constant becomes defined as a constant at that point whose value is that string. So there is some initial processing the first time the code encounters that key to create the constant, then perhaps a tiny bit of overhead on each successive iteration to grab the constant's value on each iteration.

Because of the possible collision with intentionally defined constants or built-in PHP constants (or future built-in PHP constants), you should always quote your array keys when they are string literals.

bokeh
12-30-2006, 05:40 PM
Here's a test script I just triedIn my opinion you should subtract out the loop for a better idea of the real difference between the different methods. Only looping 1000 times is not going to give accurate results especially if the server and client are the same machine. Here's your script with subtraction added: (http://bokehman.com/speed.php) <?php
$times = array();
// initialize use of microtime
$times['init'] = microtime(TRUE);
header("Content-Type: text/plain");
$test['sample'] = 'test';

$times[0] = microtime(TRUE);
for($i=1; $i<=100000; $i++);


$times[1] = microtime(TRUE);
for($i=1; $i<=100000; $i++)
{
'This is a ' . $test['sample'] . '
';
}
$times[2] = microtime(TRUE);
for($i=1; $i<=100000; $i++)
{
"This is a " . $test['sample'] . "\n";
}
$times[3] = microtime(TRUE);
for($i=1; $i<=100000; $i++)
{
"This is a {$test['sample']}\n";
}
$times[4] = microtime(TRUE);

$default = $times[1] - $times[0];

printf("Single quotes with concatenation: %.2f microseconds/cycle\n", (($times[2] - $times[1])-$default)*10);
printf("Double quotes with concatenation: %.2f microseconds/cycle\n", (($times[3] - $times[2])-$default)*10);
printf("Double quotes with interpolation: %.2f microseconds/cycle\n", (($times[4] - $times[3])-$default)*10);
?>

Brain Storm
12-30-2006, 06:24 PM
Here's your script with subtraction added: (http://bokehman.com/speed.php)Wow! That server is rock solid stable.