Valhalla Legends Forums Archive | Web Development | Basic PHP Script

AuthorMessageTime
NetworksI am quite new to PHP however I am learning through coding but I am completely stumped here.

Basically what this is, is a top 10 downloads script that will parse downloads the resulting query provides.

Code:
<?php

$db
= mysql_connect('localhost', 'db', 'pass');

$booldb = mysql_select_db('db', $db);

// if ($booldb)
// echo 'Selected and connected to the database.';

$query = "select * from table where var > 0";

$result = mysql_query($query);

$num_results = mysql_num_rows($result);

echo
'<h1>Top 10 downloads on Zeroforce:</h1>';

$Download[10] = array('name', 'downloads', 'description');

$Download = array_fill(0, 10, '');

for (
$i = 0; $i < $num_results; $i++)
{
$file = mysql_fetch_array($result);

$fileName = htmlspecialchars(stripslashes($file['file_name']));
$fileDownloads = htmlspecialchars(stripslashes($file['file_dls']));
$fileDescription = htmlspecialchars(stripslashes($file['file_desc']));

//echo $fileName.'<br>';

for ($j = 0; $j < count($Download); $j++)
{
// Set First 10 elements initially.
// First 10 elements that are checked are automatically inserted in the array.
if ($i < 11)
{
$Download[$j]['name'] = $fileName;
$Download[$j]['downloads'] = $fileDownloads;
$Download[$j]['description'] = $fileDescription;
echo "<h3>Inserting: $fileName -> $fileDownloads</h3>";
break;
}
// Validate if the next elements after the first 10 are greater then what is in the array values, replace!
else if($Download[$j]['downloads'] < $fileDownloads)
{
$Download[$j]['name'] = $fileName;
$Download[$j]['downloads'] = $fileDownloads;
$Download[$j]['description'] = $fileDescription;
echo "<h3>$Download[$j]['downloads'] ($Download[$j]['name']) is less then $fileDownloads ($fileName) </h3>";
break;
}
}
}

for (
$i = 0; $i < count($Download); $i++)
{
OutputFile ( $Download[$i]['name'],
$Download[$i]['downloads'],
$Download[$i]['description']
    );
}

function
OutputFile($name, $downloads, $description)
{
echo '<br>';
echo '<br>';
echo 'File: '.$name;
echo '<br>';
echo 'Downloads: '.$downloads;
echo '<br>';
echo 'Description: '.$description;
}
?>


Now the output I get oddly enough to show you where an error is occuring is:

Array['downloads'] (Array['name']) is less then 11 (ASP.NET)
June 26, 2005, 11:41 am
Banana fanna fo fannaI think you are not using SQL to its full potential.

Code:
<?php

$db
= mysql_pconnect('localhost', 'db', 'pass');

$booldb = mysql_select_db('db', $db);

$query = 'select file_name, file_desc, file_dls from table where var > 0 ORDER BY file_dls DESC';

$result = mysql_query($query);

$num_results = mysql_num_rows($result);

echo
'<h1>Top 10 downloads on Zeroforce:</h1>';

while (
$arr = mysql_fetch_array($result)) {
    
$file_name = $arr['file_name'];
    
$file_desc = $arr['file_desc'];
    
$file_dls = $arr['file_dls'];
    echo
"<br /><br />File: $file_name <br />Downloads: $file_dls <br />Description: $file_desc";
}

mysql_free_result($result)
?>

June 26, 2005, 07:00 pm
JTN Designermysql_pconnect is of no use in this statement, why would he need a constant connection to the database. Here is my version:

Code:
<?php
//Database Connections
$dbhost = "localhost";
$dbuser = "";
$dbpass = "";
$dbname = "";

//Connect Sequence
$link = mysql_connect($dbhost, $dbuser, $dbpass) or die(mysql_error());
    @mysql_select_db($dbname, $link) or die(mysql_error());

    $query = 'SELECT file_name, file_desc, file_dls FROM table WHERE var > 0 ORDER BY file_dls DESC';
    $result = mysql_query($query);
    $num_results = mysql_num_rows($result);
                print '<h1>Top 10 downloads on Zeroforce:</h1>';

while ((
$arr = mysql_fetch_array($result)) != FALSE) {
    $file_name = $arr['file_name'];
    $file_desc = $arr['file_desc'];
    $file_dls = $arr['file_dls'];
                print "<br /><br />File: $file_name <br />Downloads: $file_dls <br />Description: $file_desc";
}
mysql_free_result($result)
?>

June 26, 2005, 11:44 pm
Banana fanna fo fannaWell smarty pants mysql_pconnect would be an optimization for multiple page requests.

Also, add a LIMIT 10 at the end of the sql statement.
June 27, 2005, 10:45 pm
WarriorI don't really get what the advantage is in supressing mySQL errors then doing "or die" unless you're leaving a fancy message -- isn't it like doing the same thing?June 28, 2005, 11:03 am
Arta[vL]Pretty much. The (error) output would be the same, but any error (eg, a warning) would be fatal because of the call to die().June 28, 2005, 07:16 pm
Banana fanna fo fannaYou should probably die() on mysql warnings anyway, since they're often fatal in this sort of app.June 28, 2005, 09:37 pm
JTN Designer
Well smarty pants mysql_pconnect would be an optimization for multiple page requests.

Also, add a LIMIT 10 at the end of the sql statement.

<3

Just a suggestion. Smiley
June 28, 2005, 10:37 pm