I want to count the no of rows in my database..along with where clause and AND clause..
please show me how can we do that in pdo..
these are my queries:
$stmt=$dbh->prepare("SELECT * from questions where (category_id = ?) AND (complexity_id = ?) ORDER BY RAND()");
$stmt->execute(array($_POST['category'],$_POST['complexity']));
$data = $stmt->fetchAll(PDO::FETCH_OBJ)
count:
$nRows = $dbh->query("SELECT COUNT(*) FROM questions");
i need to write both queries in one query only..
you can use fetchColumn
.
$number_of_rows=$stmt->fetchColumn(); // will give no. of rows
$data = $stmt->fetchAll(PDO::FETCH_ASSOC)// give the data array
If your queries are different , like it shown in the question, then keep them as two separate queries. There is no way to get them in one. Just leave it two separate queries, it's all right.
If your queries actually the same, and you want to get your data filtered and also get the number of rows, then you don't need the second query at all. In PHP, there is a function called count(). You can use it to count results returned by your first query.
$nRows = count($data);
as simple as that
Can you just use
$stmt=$dbh->prepare("SELECT COUNT(*) from questions where (category_id = ?) AND (complexity_id = ?)");
query?
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.