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

    3 answers 3

    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

    • 6 + len (or die (mysql_error ())) - timka_s

    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

    • then PDO is better to use - tranceman
    • not necessarily ... Many people do not like PDO according to incomprehensible religious beliefs. And all that psyhitus wrote is true. **** Although I + th PDO, but the question was not about that. - Zowie
    • first heard about PDO, what is its problem? Goodies and so visible) - psyhitus

    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.

    • the whole ORM then :-) - psyhitus
    • @psyhitus, the class is enough) - Elime
    • @Elime, class is always not enough, you can generally communicate through ODBC with the base, the main thing is to know a lot about perversions) - psyhitus
    • Oh yes, a function like define ("DB_DEBUG", true); function ($ query) {$ res = mysql_query ($ query); if (! $ res && DB_DEBUG) die (mysql_error ()); return $ res; } Very heavy for php: D. @Elime - with this approach, you need to write to the assembly and not to the php Class for working with the database at the end is cool, but then it is better to have a Model, Storage, etc. factory. with basic methods. - Zowie
    • By the way - if the function 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 - Zowie