I 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.
<?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)
I think you are not using SQL to its full potential.
<?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)
?>
mysql_pconnect is of no use in this statement, why would he need a constant connection to the database. Here is my version:
<?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)
?>
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.
I 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?
Pretty much. The (error) output would be the same, but any error (eg, a warning) would be fatal because of the call to die().
You should probably die() on mysql warnings anyway, since they're often fatal in this sort of app.
Quote from: Banana fanna fo fanna on June 27, 2005, 10:45 PM
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. :)