For adding database I am using PhpMyAdmin in Xampp server. I have created two tables and each table contains different rows. I am trying to select values from two tables at the same time in php. The following code works fine
$gi1=2;
$stmt2=$this->con->prepare(" SELECT qwer FROM table1 WHERE nmnm=?;");
$stmt2->bind_param("i",$gi1);
$stmt2->execute();
$stmt2->bind_result($var1);
$stmt2->fetch();
$gi2=23;
$stmt2=$this->con->prepare(" SELECT qwer2 FROM table2 WHERE nmnm2=?;");
$stmt2->bind_param("i",$gi2);
$stmt2->execute();
$stmt2->bind_result($var2);
$stmt2->fetch();
But it takes too much time so I decided it to select in one statement.
$gi2=23;$gi1=2;
$stmt2=$this->con->prepare(" SELECT qwer2,qwer1 FROM table2,table1 WHERE nmnm2=? or nmnm=?;");
$stmt2->bind_param("ii",$gi2,$gi1);
$stmt2->execute();
$stmt2->bind_result($var2,$var1);
$stmt2->fetch();
And it shows a fatal error.So how can I select values from two tables in one statement?
if the tables do not have any link columns you can try with UNION
SELECT qwer FROM table1 WHERE nmnm=?
union
(SELECT qwer2 as qwer FROM table2 WHERE nmnm2=?)
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.