What is the best way to access the database?
1) via function call
function query($request) { $result = mysql_query($request) or die(mysql_error()); return $result; }
2) straight from the file
$result = mysql_query('...');
What is the best way to access the database?
1) via function call
function query($request) { $result = mysql_query($request) or die(mysql_error()); return $result; }
2) straight from the file
$result = mysql_query('...');
More flexible and correct option from 2 provided - the first. I will give a simple example:
Let's say a lot of code has already been written using the samopisnaya and native query functions. We use everywhere in the code (let's say, o5) or die(mysql_error())
. And here we are, clearly with grief in half, if we work with the database this way, well, yes, it’s not the point, we’ve finished - if there is an error and we don’t have to output it. To do this, we need to scrape through every single script and everywhere comment out or delete the or die(mysql_error())
part or die(mysql_error())
.
In this case, as for the samopisnaya function, it is enough to delete or comment out or die(mysql_error())
1 time and enjoy life.
Already not nice really? But suppose we wrote with the use of mysql_query
, removed or die (mysql_error ()) and it still taught us nothing ...
Suppose we have the need to log all (or specific) mysql errors. No matter how we decide to do this - by calling the samopisnaya functions, or by code - we will have to climb through the scripts and edit, edit, edit ...
When using the self-writing function, it suffices to add a logging call 1 time.
Well, so to speak, apofigay so to speak - let's say we need to transfer everything to another DBMS ... I don’t think it should be continued - all the same.
PS: and the programmer should be lazy, why constantly write mysql_query (), if you can just query () - 6 extra bukaff: D
The first option is better, because you may always need to change the subd, as it is possible to work out exceptional situations in it
If the speed of the script execution is important to you, use the second option. If convenience, use the first. For general convenience, I would recommend using the class for working with the database, rather than separate functions.
query()
slows down the work - what does not slow it down? : D According to your logic, the code of the form is function hello ($ name) {echo 'Hello'. $ name;} hello ('World'); // Khan's server: DDD - ZowieSource: https://ru.stackoverflow.com/questions/66856/
All Articles