QUESTION 1
Okay so I need to clear a confusion. I have multiple queries on the same page like this.
$abc = $pdo->prepare("SELECT COUNT(*) FROM tablename");
$abc-> execute();
$count1 = $abc->fetchColumn();
$bcd = $pdo->prepare("SELECT COUNT(*) FROM tablename");
$bcd-> execute();
$count2 = $bcd->fetchColumn();
$def = $pdo->prepare("SELECT COUNT(*) FROM tablename");
$def-> execute();
$count3 = $def->fetchColumn();
This works fine. But declaring a new name for each query somewhat gives me an irritation. Therefore, I assumed that wouldn't it be nice if all the queries could be written in the same variable? So, will it be wrong if I write these queries in one variable only? Like the following code.
$stmt = $pdo->prepare("SELECT COUNT(*) FROM tablename");
$stmt-> execute();
$count1 = $stmt->fetchColumn();
$stmt = $pdo->prepare("SELECT COUNT(*) FROM tablename");
$stmt-> execute();
$count2 = $stmt->fetchColumn();
$stmt = $pdo->prepare("SELECT COUNT(*) FROM tablename");
$stmt-> execute();
$count3 = $stmt->fetchColumn();
Will writing the queries in the same way give any errors in the future? Is it safe to write in the same variable? Is there any difference? Which method is better?
QUESTION 2
To get $count1, $count2 and $count3
I had to pass three different queries. But I prefer writing as less query as possible to prevent the use of much resources. So, is it necessary to be done this way? Or, is it possible to get them all in one single query? If yes, then how?
Please help me clear these doubts devs.