Simplify (shorten) this code.

$result = mysql_query("SELECT name FROM `pages`"); $r = mysql_fetch_array($result); $name = $r['name']; 

    3 answers 3

     $name = mysql_fetch_array(mysql_query("SELECT name FROM `pages`"))['name']; 

    See? =)

    • makes sense, and why not? (this is to Deonis'u) - Smash
    • @Viczech, and what is it good? You can serve all the dishes of a particular type, or you can mix a hodgepodge, a Russian salad, and throw the kebabs in the same plate and season with compote. Well, why not? Indeed, in the stomach is still mixed up. PS @Gorets, I’d give my hands for teaching the bad to the little ones)) - Deonis
    • one
      @Deonis, I am ashamed of myself. Dear gentlemen, novice programmers, never-never write as I showed you, this is a sample of how not to do it. - Gorets

    Write your class to work with the database, there make a function that will select the specified field as a parameter. Will you code in one line.

       $name=mysql_result(mysql_query("SELECT name FROM `pages`"),0,'name');