I have 4 times applied on the page in my code ... mysql_query ("SELECT * FROM ...

$result = mysql_query("SELECT * FROM types WHERE `id`='$rrrr'",$db); $result4 = mysql_query("SELECT * FROM `aaaa`, `link` WHERE `link`.`article_id`='$page' AND `link`.`tags_id`=`tags`.`id`",$db); $result = mysql_query("SELECT * FROM types WHERE `id`='$rrrrr'",$db); $result = mysql_query("SELECT * FROM types WHERE `id`='$rrrrr'",$db); 

Is it correct to write code like this? Will this code not slow down the loading of the page with a big visit?

    3 answers 3

    For the database, such queries are minor. In your situation, such a minicode is also not dangerous. But I want to go with an appeal to all novice clerks in PHP, do not accept this as criticism, but adopt it! Yes, this scripting language is democratic, you can add amateurish things, you can even make mistakes sometimes ... BUT! Over time, as knowledge is added, the code will increase and become more complicated, it will become harder to remember, and then, in addition to problems with a headache, you will have problems with the speed of the code, security problems. There is a writing style that PHP developers came up with called "Zend" I advise you to read. Remember! A good programmer must have good code!

    • Zend is an OOP lover. Personally, I have not yet met a single web project with a budget of up to 1 million rubles, in which the PLO is justified by anything. - 1232
    • To assume that Zend is just OOP is a profound delusion. If you have a wealth of experience in web projects, can you recommend your sources? - NeonDT
    • I did not say the word "only". - 1232

    Problems:

    1) The names of variables ( $result, $rrrrr ), tables ( aaaa, types, link ) are extremely "informative"

    2) Why double the same query?

     $result = mysql_query("SELECT * FROM types WHERE id='$rrrrr'",$db); $result = mysql_query("SELECT * FROM types WHERE id='$rrrrr'",$db); 

    Depending on the real need, you can do this:

     $query_types = mysql_query( "SELECT * FROM types WHERE id in ( '$rrrr', '$rrrrr')", $db ); 
    • here the syntax is not important .. I just want to theoretically find out ... when here so 4 times on the page SELECT FROM .... do you use this much will it slow down the base? so generally you can write humanly? - qwerty17
    • one
      On php, how not to write - they still call the govnokod ....) Personally, I think that we need to look for the smallest evil out of the number of requests and the amount of memory consumed. Those. it all depends on the situation. - 1232

    The site database has to store a lot of different information and when generating the current page, you must choose from different tables: title, author and content of this page; select the desired template; do not forget comments on the page; random quote; random photos on the page; refer to the visitor counter; show TOP10 popular pages; to show information about the current user and much more have to be taken from different tables when dynamically compiling the page.