PHP Mysql PDO query from foreach multiple values stored for output -
today i've been searching on how store different values query injected 4 times different categories. result wanted got values when return them last query result. i'm wondering outcome problem
public function setmaxid() { $categorys = $this->getcategorys(); foreach ($categorys $category) { $sth = $this->conn->prepare("select max(data_id) bcc_data data_category = '" . $category['bcc_data_category_name'] . "'"); $sth->execute(); $id = $sth->fetchall(); $maxid = $id[0]['max(data_id)']; var_dump($maxid); } } var_dump:
string '22' (length=2) string '35' (length=2) string '34' (length=2) string '29' (length=2)
it looks overwriting max id each time in loop, if want return them this:
$maxids = []; foreach ($categorys $category) { $sth = $this->conn->prepare("select max(data_id) bcc_data data_category = '" . $category['bcc_data_category_name'] . "'"); $sth->execute(); $id = $sth->fetchall(); $maxids[] = $id[0]['max(data_id)']; } return $maxids; //or return max($maxids) if want single max
Comments
Post a Comment