How bad is my PHP scripting?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Todd
Forum Newbie
Posts: 2
Joined: Mon Aug 26, 2002 4:04 am

How bad is my PHP scripting?

Post by Todd »

Ok,

I am pretty new at this PHP thing.

I was wondering if someone could look at a script that I made and tell me how poorly written it is and give me suggestions on what I could or should have done to make it better or more simple.

Here is the link to my script:
myscript.txt

Thank you!

Todd
User avatar
Xelmepa
Forum Commoner
Posts: 41
Joined: Sat Aug 24, 2002 3:02 pm
Location: Athens, Greece
Contact:

Post by Xelmepa »

You've have made quite a few mistakes...
First of all, MySQL:

Connection to MySQL should be done like this:

Code: Select all

$connection = mysql_connect("localhost","username","password");
This way you can close the MySQL connection when you won't need it anymore. Also, you have opened the connection in the beggining of the script, and actually used it at the mid-end, which just leaves the connection open for some time. You should make the connection when you need it...

Also, you have done around a hundred print(); functions, that you dont need to. You can as well just close PHP when you really don't need it (just use "?>"), throw in all the HTML, and start php again (using "<?"), instead of calling print(); all these times...

These are the major faults I can see.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

mysql_connect("localhost","username","password");
mysql_select_db("database");
$sql = mysql_query("select * from trade_skills ORDER BY '$field' $order");
your select-string uses $field and $order. if register_globals is Off (in newer versions of php) you have to get the values from $_GET['field'] and $_GET['order'];

tip:
use .... or die(mysql_error()); expressions while developing i.e.
mysql_select_db("database") or die(mysql_error());
this will display the reason why select_db failed (if) and abort the script.

If it complains about a query-string, assign that query to a var. first and print it out, too.
i.e. $result = mysql_query($query) or die ($query . ' -> '. mysql_error());

sometimes you may want to get some "debug-informations" but do not want to interfere with the html-output-layout. Then it's time to use html comments ;) i.e

Code: Select all

while($row = mysql_fetch_row($result))
&#123;
   $dbgOut = var_dump($row);
   $dbgOut = htmlentities($dbgOut); // not always the best solution
   print('<!-- ' . $dbgOut . ' -->');
   ...
Todd
Forum Newbie
Posts: 2
Joined: Mon Aug 26, 2002 4:04 am

Thnak you for you input!

Post by Todd »

It is a real help!


thank you!

Todd
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post by Takuma »

Code: Select all

Blah Blah Blah ("select * from trade_skills ORDER BY '$field' $order");
I don't think you need '$field', I think it can be just $field without 's.
User avatar
gotDNS
Forum Contributor
Posts: 217
Joined: Tue May 07, 2002 5:53 pm
Location: West Chester, PA

Post by gotDNS »

NEWS FLASH: Forget PRINT! now use ECHO!

:: shrug :: ... it looks kooler
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

I like prefix-function-notation :D
print() forever
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post by Takuma »

What's the difference between print & echo?
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

Pretty much: Nothing.
codewarrior
Forum Commoner
Posts: 81
Joined: Wed Aug 07, 2002 1:28 pm

Post by codewarrior »

huh? :?: Still learning :D
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

What is the difference between echo and print?

Which is faster, echo or print?

Jun 8th, 1999 09:00

Nathan Wallace
Rasmus Lerdorf



There is a difference between the two, but speed-wise it
should be irrelevant which one you use. print() behaves
like a function in that you can do:

Code: Select all

$ret = print "Hello World";
And $ret will be 1

That means that print can be used as part of a more complex
expression where echo cannot. print is also part of the
precedence table which it needs to be if it is to be used
within a complex expression. It is just about at the bottom
of the precendence list though. Only "," AND, OR and XOR
are lower.

echo is marginally faster since it doesn't set a return
value if you really want to get down to the nitty gritty.

If the grammar is:

Code: Select all

echo expression &#1111;, expression&#1111;, expression] ... ]
Then

Code: Select all

echo ( expression, expression )
is not valid. ( expression ) reduces to just an expression
so this would be valid:

Code: Select all

echo ("howdy"),("partner");
but you would simply write this as:

Code: Select all

echo "howdy","partner";
if you wanted to use two expression. Putting the brackets
in there serves no purpose since there is no operator
precendence issue with a single expression like that.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

of course, one may post the complete article here, too :D
User avatar
phice
Moderator
Posts: 1416
Joined: Sat Apr 20, 2002 3:14 pm
Location: Dallas, TX
Contact:

Post by phice »

Also, ya'll forgot about his if...else... coding..

Code: Select all

if($order == "DESC") 
$order = "ASC"; 
else 
$order = "DESC";
it should be...

Code: Select all

if ($order == "DESC") &#123;
unset($order);
$order = "ASC";
&#125; else &#123;
if (isset($order)) &#123; unset($order) &#125;
$order = "DESC";
&#125;
Image Image
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

there is no need to unset them...
Post Reply