Here is my code
$sql1 = "SELECT * FROM user_follow WHERE user = :user";
$stmt1 = $conexao_pdo->prepare($sql1);
//where clause
$stmt1->bindParam(':user', $username);
$stmt1->execute();
while ($row1 = $stmt1->fetch(PDO::FETCH_ASSOC))
{
echo '</br>Followers: </br>'.$row1['followers'].'</br>';
}
It show the name of the followers but I would like a implementation in PDO to also show the amount in numbers of the $row1['followers']
If you just need the count you can use rowCount() function:
$stmt1->execute();
$count = $stmt1->rowCount();
echo '</br>Followers: </br>'.$count.'</br>';
so it's just echo $count
Related
I have a sql database where I request the following bit of code;
SELECT albumtype, COUNT(*) FROM albumdata GROUP BY albumtype
The response in phpMyAdmin is the following table
albumtype | COUNT(*)
Album | 4
EP | 1
Single | 1
Then I have in my php file the following code that will return the complete count (6).
$stmt = $con->prepare('SELECT albumtype, COUNT(*) FROM albumdata GROUP BY albumtype');
$stmt->execute() or die("Invalid query");
$result = $stmt->get_result();
$row = $result->fetch_assoc();
$row_cnt = mysqli_num_rows($result);
I used this code on another page, but now I want to select a specific part of the "count()" table.
I tried to display a single result with $row_cnt = $row['Album'];, but as it turns out, this returns "Array" for some reason. Here is my php call:
$stmt = $con->prepare('SELECT albumtype, COUNT(*) FROM albumdata GROUP BY albumtype');
$stmt->execute() or die("Invalid query");
$result = $stmt->get_result();
$row = $result->fetch_assoc();
$row_cnt = $row['Album'];
How can I grab a single row, for example the number of how much the database could find Album (4 times) and put it in a php variable? I tried searching it on here, but didn't get any further.
1.If you want only specific albumType ten you can directly change your query like this:-
$stmt = $con->prepare("SELECT albumtype, COUNT(*) as counts FROM albumdata WHERE albumtype = 'Album'");
$stmt->execute() or die("Invalid query");
$result = $stmt->get_result();
$row = $result->fetch_assoc();
$album_cnt = $row['counts'];
echo $album_cnt;
But if you want all then,you need to do it like below:-
$stmt = $con->prepare('SELECT albumtype, COUNT(*) as counts FROM albumdata GROUP BY albumtype');
$stmt->execute() or die("Invalid query");
$result = $stmt->get_result();
$row_cnt = array();
while($row = $result->fetch_assoc()){
$row_cnt[$row['albumtype']] = $row['counts'];
}
echo "<pre/>";print_r($row_cnt);
// you have all data in array so you can use it now like below
foreach($row_cnt as $key=>$value){
echo "Album type ".$key." has ".$value." counts"."<br/>";
}
//form this all data if you want to compare specific albumType then do like below:-
foreach ($row_cnt as $key=>$value) {
if($key == 'Album'){
echo $value;
}
}
Loop over the rows till you match the type you want to display:
foreach ($row as $srow) {
if($srow['albumtype'] == 'Album'){
print $srow['count'];
}
}
So I'm doing this query:
'SELECT * FROM Album WHERE id = ?;'
using a prepare sql statement, and I was wondering how to get the number of results this query returns? B/c since every album id is unique this query should only return 1 album and I want to make sure that its doing that.
Code
$stmt = $mysqli->prepare('SELECT * FROM Album WHERE id = ?;');
$id = 2;
$stmt->bind_param('i', $id);
$executed = $stmt->execute();
$result = $stmt->get_result();
if($row = $result->fetch_assoc()){
$info['id'] = $row['id'];
$info['title'] = $row['title'];
$info['date_created'] = $row['date_created'];
$info['creator'] = $row['creator'];
}
// Send back the array as json
echo json_encode($info);
You can get it using $result->num_rows:
if ($row = $result->fetch_assoc()) {
$info['id'] = $row['id'];
$info['title'] = $row['title'];
$info['date_created'] = $row['date_created'];
$info['creator'] = $row['creator'];
/* determine number of rows result set */
$row_cnt = $result->num_rows;
printf("Result set has %d rows.\n", $row_cnt);
}
You can find more details on PHP Documentation.
I want to make sure that its doing that
In your code you are already doing that. The following condition
if($row = $result->fetch_assoc()){
does exactly what you want.
Just use COUNT(*)
SELECT COUNT(*) FROM Album WHERE id = ?;
Reference:
https://dev.mysql.com/doc/refman/5.7/en/counting-rows.html
Another way
$query = $dbh->prepare("SELECT * FROM Album WHERE id = ?");
$query->execute();
$count =$query->rowCount();
echo $count;
i have this function separated in my working page.
public function countRow(){
$id = $_SESSION['id'];
$num = 1;
$query = "SELECT count(*) from `auditsummary` where bizID=? AND statusID=?";
$sql = $this->db->prepare($query);
$sql->bindParam(1,$id);
$sql->bindParam(2,$num);
$sql->execute();
}
what i'm really trying to do in this function is to count the number of rows that are results of the query but i don't know how to do it and also how to return the value.
As you use a PDOStatement for your query, after the execute, you can use
$count = $sql->rowCount();
More information:
http://php.net/manual/en/pdostatement.rowcount.php
And to return the result, you can just do:
return $count;
Information for this:
http://php.net/manual/en/function.return.php
Use
$query = "SELECT count(*) AS getCount from `auditsummary` where bizID=? AND statusID=?";
And get the values as you normally does
$count = $row["getCount"];
Here's how I do it:
$count = "SELECT * FROM yourtable WHERE x='x' and y='y'";
$result = $dbconn->prepare($count);
$result->execute();
$t_count = $result->rowCount();
echo $t_count;
I am trying to do a row count, I want to count a row (nummer) and if there is more then 1 row with the same number then echo. but no matter how many rows I have in my tabel, it only returs 0
$nummer = $_GET['nummer'];
$pdo = Database::connect();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$result = $pdo->prepare("select count(*) from rum where nummer=:n");
$result->bindParam(':n', $nummer, PDO::PARAM_INT);
$result->execute();
$rows = $result->fetchAll;
if(count($rows) >1) {
echo "1";}
else {
echo "0";
}
The following statement
$result = $pdo->prepare("select count(*) from rum where nummer=:n");
will always return one row with the count number, so doing
$rows = $result->fetchAll;
will always return one.
You may do as
$result = $pdo->prepare("select count(*) as tot from rum where nummer=:n");
....
....
$rows = $result->fetchAll();
if($rows["tot"] > 0 ){
echo 'something'
}else{
echo 'something else'
}
just use fetch() instead of fetchAll()
$rows = $result->fetch();
if($rows[0]) >1) {
echo "1";
} else {
echo "0";
}
It looks like you're having two mistakes.
First one: If you're querying for COUNT(*) - you are getting a number of rows. For example: You're return will be a field named COUNT(*) with one row containing the number of rows found.
If you replace
$result = $pdo->prepare("select count(*) from rum where nummer=:n");
with
$result = $pdo->prepare("select * from rum where nummer=:n");
Second one:
Replace
$rows = $result->fetchAll;
With
$rows = $result->fetchAll();
fetchAll() is no property but a method.
The other way would be your query but naming the field (MySQL AS keyword) and calling $rows = $result->fetch(); afterwards and checking $rows->fieldName for the number of found rows.
Use PDOStatement::fetchColumn(). It is useful for "one-column" resultsets (which relatively often produced by queries like SELECT COUNT(...) FROM ...). Example:
$nummer = isset($_GET['nummer']) ? $_GET['nummer'] : null;
$pdo = Database::connect();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $pdo->prepare('SELECT COUNT(*) FROM rum WHERE (nummer = :n)');
$stmt->bindParam(':n', $nummer, PDO::PARAM_INT);
$stmt->execute();
$rows = $stmt->fetchColumn();
echo $rows > 1 ? "1" : "0";
How would i do this using mysqli?
$SQL = mysql_query("SELECT username FROM users WHERE username = '$new_username'");
$result = mysql_num_rows($SQL);
If(!$result > 0) {
echo...
I tried:
$SQL = $mysqli->query("SELECT username FROM users WHERE username = '$utilizator");
$rezultat->num_rows($SQL);`
But I dont get any result.
You have started the query with $SQL .. continue with it as the object will be in that variable:
$SQL = $mysqli->query("SELECT username FROM users WHERE username = '$utilizator'");
$num = $SQL->num_rows();
if($num){
// run your code here
}
you have to call store_result() after execution.
php.net has a wonderful php doc: http://www.php.net/manual/de/mysqli-stmt.num-rows.php
for example:
$query = "SELECT Name, CountryCode FROM City ORDER BY Name LIMIT 20";
if ($stmt = $mysqli->prepare($query)) {
/* execute query */
$stmt->execute();
/* store result */
$stmt->store_result();
printf("Number of rows: %d.\n", $stmt->num_rows);
/* close statement */
$stmt->close();
}
$number_of_rows = $SQL->num_rows;