Valhalla Legends Forums Archive | Web Development | Basic PHP Script

AuthorMessageTime
Networks
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.

[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;
}
?>
[/code]

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, 4:41 PM
St0rm.iD
I 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)
?>
[/code]
June 27, 2005, 12:00 AM
JTN Designer
mysql_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)
?>
[/code]
June 27, 2005, 4:44 AM
St0rm.iD
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.
June 28, 2005, 3:45 AM
Quarantine
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?
June 28, 2005, 4:03 PM
Arta
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 29, 2005, 12:16 AM
St0rm.iD
You should probably die() on mysql warnings anyway, since they're often fatal in this sort of app.
June 29, 2005, 2:37 AM
JTN Designer
[quote author=Banana fanna fo fanna link=topic=11983.msg117841#msg117841 date=1119930338]
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.
[/quote]

<3

Just a suggestion. :)
June 29, 2005, 3:37 AM

Search