Click to See Complete Forum and Search --> : Functions


benson
12-16-2003, 03:44 PM
So give me critique :D

<?php
include "http://www.streetfreaks.nl/includes/db.php";
#----------------------------Verklaring-------------------------------------------#
#vb. nick("user_level=3","<br>"); #
#vb. nicklogin("20","<br>"); 20 staat voor het aantal echo's #
#vb. verjaardag(); stuurt altijd mail naar een persoon die jarig is op dat moment.#
#---------------------------------------------------------------------------------#



function nick($a,$b)
{
$sql = mysql_query("SELECT nicknaam FROM users WHERE ".$a);
while ($row = mysql_fetch_object($sql))
{
echo $row->nicknaam.$b;
}
mysql_free_result();
mysql_close();
}
function nicklogin($a,$b)
{
$sql = mysql_query("SELECT nicknaam FROM users SORT BY last_visit LIMIT $a");
while ($row = mysql_fetch_object($sql))
{
echo $row->nicknaam.$b;
}
mysql_free_result();
mysql_close();
}
function verjaardag()
{
$sql = mysql_query("SELECT email_adres FROM users WHERE geb_datum=date(j-n-Y)");
$check = mysql_num_rows($sql);
if ($check > 0)
{
while ($row = mysql_fetch_object($sql))
{
$to = $row->email_adres;
$sub = "Gefeliciteerd";
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "From: Streetfreaks.nl <streetfreaks@streetfreaks.nl>\r\n";
$message = "
Gefeliciteerd !!
<img src=\"http://www.streetfreaks.nl/images/verjaardag.jpg\">
";
mail($mailto, $subject, $message, $headers);
}
}
mysql_free_result();
mysql_close();
}
?>

LordShryku
12-16-2003, 04:11 PM
Well, personally when I'm making functions, or just using any variables, there's a meaning behind the naming of the variable. Sure, you may have to type a couple extra letters(oh my!), but at least I know what each one is. The whole $a and $b thing just doesn't work. Also, you may want to show an example of how these will work. SELECT nicknaam FROM users WHERE ".$a means you're not just passing a name to the function, but also a fieldname, and some formatting.

Shrike
12-17-2003, 09:00 AM
$sql = mysql_query("SELECT email_adres FROM users WHERE geb_datum=date(j-n-Y)");

will pass 'date(y-n-Y)' to MySQL as a literal string? :)

Tristan Wells
12-17-2003, 09:02 AM
mysql_fetch_object will slow you script eventually.

planetsim
12-17-2003, 05:19 PM
Not to mention when using OOP you can confuse yourself and reuse object variables. Try to use mysql_fetch_array()

superwormy
01-05-2004, 12:13 AM
No database abstraction layer?

No checking of vars for malicios input before you use them in queries?

Calling mysql_close() on a connection that, from what I've seen, hasn't even been opened?

Ugly indents ( but that might just be the forum )?

I personally hate functions that just echo() data too, its nicer if they return; it so that I can manipulate it later and its a little more reusable, but thats just a matter of prefernece.