Server resources

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

Post Reply
Alcor
Forum Newbie
Posts: 2
Joined: Tue Jul 22, 2003 7:34 pm
Location: QC, Canada

Server resources

Post by Alcor »

I have a PHP server and I'm developping a Secure web site with data base transactions. Perhaps my question look stupid because I'm starting with PHP.

When I create my PHP script for DB transactions, is it better to create a big script with all the Add, Mod, Del, ... command or to create a number of smaller script with each DB transactions in it? I want to know that because I need to optimise the answer delay on my server.

If you have informations, let me know !

Thanks !

Sorry for my english, I'm french Canadian ! :wink:
qartis
Forum Contributor
Posts: 271
Joined: Sat Dec 14, 2002 4:43 pm
Location: BC, Canada
Contact:

Post by qartis »

By "DB transactions", do you mean the individual queries, or functions FOR DB transactions? Unless you're developing a gigantic application, it's safe to have the connection calls included at the beginning, individual queries by themselves, and close the db connection at the end (especially if you're new to php)

PS: On peut parler du francais quebecois, si vous voulez :)
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post by Gen-ik »

If you want to keep things tidy and fast you can always create a collection of .php files ( such as mod.php, del.php, add.php etc ) for your database functions and then just include() the ones you need when you need them.

Here's an example......

This is the add.php file

Code: Select all

<?php
function add_to_database($table, $name, $email)
{
mysql_query(" INSERT INTO $table VALUES ('$name','$email') ");
}
?>
And this is part if the main page...

Code: Select all

<?php
If(isset($action) && $action=='add')
{
include("add.php");
add_to_database('tablename','bob','bob@home.co.uk');
}
?>
Something like that anyway :D

Using include() is always good because it means you don't have to load up every single function and/or php code at once, and it normally (if used in the right way) will speed up your page run/load times.
qartis
Forum Contributor
Posts: 271
Joined: Sat Dec 14, 2002 4:43 pm
Location: BC, Canada
Contact:

Post by qartis »

Code: Select all

<?php
function add_to_database($table, $name, $email)
{
mysql_query(" INSERT INTO $table VALUES ('$name','$email') ");
}
?>
Good principle, bad example :)

If you don't need to be so specific (and want to avoid an entire class), you should be able to get away with something like

/inc/db/select.php:

Code: Select all

<?
function db_select($feilds_array,$table,$where){
	foreach($feilds_array as $name){
		(isset($feilds))?$feilds.=",";
		$feilds.="`$name`";
	}
	return (mysql_query("SELECT $feilds FROM `$table` WHERE $where"))?true:false;
}
?>
Don't use that, though, it's probably full of errors. I just wanted to type something up to show you that it's *possible*, but not really necessary (YMMV)
Alcor
Forum Newbie
Posts: 2
Joined: Tue Jul 22, 2003 7:34 pm
Location: QC, Canada

Thanks !

Post by Alcor »

Thanks for your answers qartis and Gen-ik. It's appreciated.
When I will be better with PHP, I will undoubtedly come here to help other PHP developper.

Thanks a lot !!!
Post Reply