• Welcome to Valhalla Legends Archive.
 

Basic PHP Script

Started by Networks, June 26, 2005, 11:41 AM

Previous topic - Next topic

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.


<?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)

Banana fanna fo fanna

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)?>


JTN Designer

#2
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)?>

(advertising deleted by forum administrator)

Banana fanna fo fanna

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.

Warrior

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?
Quote from: effect on March 09, 2006, 11:52 PM
Islam is a steaming pile of fucking dog shit. Everything about it is flawed, anybody who believes in it is a terrorist, if you disagree with me, then im sorry your wrong.

Quote from: Rule on May 07, 2006, 01:30 PM
Why don't you stop being American and start acting like a decent human?

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().

Banana fanna fo fanna

You should probably die() on mysql warnings anyway, since they're often fatal in this sort of app.

JTN Designer

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. :)
(advertising deleted by forum administrator)