Re: Query in Query problems

From: Date: Thu, 17 Apr 2008 00:58:16 +0000
Subject: Re: Query in Query problems
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-273131@lists.php.net to get a copy of this message
VamVan wrote:
Hello All, We many times encounter the situations of having Queries inside loop of another query. Many times we can solve the issue by query joins but there will be some situations where we cannot do it. For Example: function Change($id){
        $qry_reg = "SELECT registrationID FROM registration  WHERE event_id
= '$id'";
        $res_reg =& $this->mdb2->query($qry_reg);
        while ($row_reg = $res_reg->fetchRow()) {
            $userid = $row_reg['registrationid'];
            $sql_insert = "INSERT
               INTO
               run
               (command, event_id, created, user_id, runtime)
               VALUES
               ('topic_change', '".$id."', NOW(), '".$userid."', NOW())";
                $res_insert =&  $this->mdb2->query($sql_insert);
        }
    }
In this example you can just use a single query: $query = "insert into run(command, event_id, created, user_id, runtime) select 'topic_change', registrationID, NOW(), ".(int)."$userid, NOW() FROM registration WHERE event_id='".(int)$id."'"; -- Postgresql & php tutorials http://www.designmagick.com/

« previous php.general (#273131) next »