php - Loop through all the inventory database and join three tables to one array -
i have inventory of cars in splitted in 3 different tables each car having same aut_id
on 3 tables.
im trying loop through cars while ($counter <= $autocount)
meaning every car, , info 3 tables each car display data mileage
, price
here code far
while ($counter <= $autocount) //ie run every car { $sql = "select * `auto` `auto_id` > $auto_id , `sold` = $soldvalue"; $get = $dbh->prepare($sql); $get->execute(); $sql1 = "select * `attributes` `auto_id` > $auto_id , `sold` = $soldvalue"; $get1 = $dbh->prepare($sql1); $get1->execute(); $sql2 = "select * `pictures` `auto_id` > $auto_id , `sold` = $soldvalue"; $get2 = $dbh->prepare($sql2); $get2->execute(); $attributes = $auto = $pictures =
my final solution @richarda
// cars, join attributes , pictures according // `auto_id` $query = "select * `auto` inner join `attributes` on `auto`.`auto_id` = `attributes`.`auto_id` inner join `pictures` on `auto`.`auto_id` = `pictures`.`auto_id` `auto`.`soldvalue` = :soldvalue , `auto`.`autoid` > :autoid"; // prepare query, binding parameters prevent sql injections $getautos = $dbh->prepare($query); // bind autoid $getautos->bindparam(":autoid", $auto_id, pdo::param_int); // bind soldvalue $getautos->bindparam(":soldvalue", $soldvalue, pdo::param_int); // execute query $getautos->execute(); // fetch cars now, getting associative array $autos = $getautos->fetchall(pdo::fetch_assoc); // loop through each car foreach($autos $auto) { // display cars here }
you have fetch result fetching them , store them in variable
$attributes = $get->fetch(pdo::fetch_assoc); $auto = $get1->fetch(pdo::fetch_assoc); $pictures = $get2->fetch(pdo::fetch_assoc);
hence
$attribute[keyname / index name / column name] = value $auto[keyname / index name / column name] = value $pictures[keyname / index name / column name] = value
Comments
Post a Comment