简体   繁体   中英

is there any better code (MySql or PHP) where i can fetch two table in one statement or query?

the 'Do' method is for prepared statement and it returns (by fetchall()), i am using two statement which is looping (foreach()), is there any better code (Sql) where i can fetch two table in one statement or query? if no then what can i do to make this code look better. (PHP)

// 'id' is index in 'userinfo' table and 'id' in 'election' table is a child index(foreign key)
// 'ElectionId' is also the index, but it is not connected with othertable (only 'id' is linked)   

 <?php
    $call = new world();
    $get = $call->Do("SELECT Votes, Views, id FROM election WHERE ElectionId = ?", array("electionIdUknwn"));
        foreach($get as $show)
            {
                <div>"Votes is :".$show[0]</div>
                $add = $call->Do("SELECT username FROM userinfo WHERE id = ?", array($show[2]) );
                foreach($add as $display)
                {
                    <div>"Username is :".$display[0]</div>
                }
                <div>"Views are :".$show[1]</div>
            }         
  ?>
SELECT e.Votes, e.Views, e.id, u.username
FROM   election e JOIN userinfo u USING (id)
WHERE  e.ElectionId = ?
SELECT e.Votes, e.Views, e.id, u.username
FROM election e INNER JOIN userinfo u ON u.id = e.id
WHERE e.ElectionId = ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM