I am trying to run this Query:
$stmt = $conn->prepare("SELECT COUNT(*) as a from session ");
$stmt->execute(array());
$records = $stmt->fetch();
echo '- '.$result["a"];
but its just displaying:
-
on its own, with no number of records, i know there is rows because when i run
SELECT COUNT(*) from `session` as a
in PHPMyAdmin it shows all the rows there in the column a
why would this query not work?
Here you go for a single column result you can use fetchColumn() and also you have aliased the table name not the column name
$stmt = $conn->prepare("SELECT COUNT(*) as a from `session`");
$stmt->execute(array());
echo $stmt->fetchColumn();
PDOStatement::fetchColumn
try this
$stmt = $conn->prepare("SELECT COUNT(*) as cnt from `session` ");
$stmt->execute(array());
$records = $stmt->fetch();
echo '- '.$records["cnt"];
why you are echoing $result.. you havent declared it..
use $records
echo '- '.$records["a"];
Try like this
$stmt = $conn->prepare("SELECT COUNT(*) as a from `session` ");
$stmt->execute(array());
$records = $stmt->fetch();
echo '- '.$records["a"];
Related
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
I am trying to show the number of rows found in the database after the searching.
Here is my code:
$city = $_POST['city'];
$bloodType = $_POST['donorType'];
$q = $db->prepare("SELECT count(*) FROM `users` AS numusers WHERE `city` = :city AND `bloodType` = :bloodType");
$q->bindValue(":city",$city,PDO::PARAM_INT);
$q->bindValue(":bloodType",$bloodType);
$q->execute();
while($row = $q->fetch(PDO::FETCH_ASSOC)){
echo "<p align='center'><h5> There is/are <span class='red-text'>".$row['numusers']."</span> available donor(s) found.
You must be a <b><a href='register.php'>registered user</a></b> to view their details.</h5></p>";
}
That's the last try I did. And I get this error message
`Notice: Undefined index: numusers
How do I solve that Tips ?
Just create alias of count(*)
SELECT count(*) AS numusers..
It would be
$q = $db->prepare("SELECT count(*) AS numusers FROM `users` WHERE `city` = :city AND `bloodType` = :bloodType");
There is a special method in PDO to retrieve this kind of data - a single value returned by the query, PDOStatement::fetchColumn(). So you don't have to hassle with aliases at all.
Besides, a while loop is superfluous here.
$city = $_POST['city'];
$bloodType = $_POST['donorType'];
$q = $db->prepare("SELECT count(*) FROM `users` WHERE `city` = :city AND `bloodType` = :bloodType");
$q->bindValue(":city", $city, PDO::PARAM_INT);
$q->bindValue(":bloodType", $bloodType);
$q->execute();
$numusers = $q->fetchColumn();
echo "<p align='center'><h5> There is/are <span class='red-text'>$numusers</span> available donor(s) found.
You must be a <b><a href='register.php'>registered user</a></b> to view their details.</h5></p>";
i have a query
$result = mysql_query("SELECT * FROM comprofiler WHERE cb_playstationgames LIKE '%FIFA%' ORDER BY id ASC");
in that query there is a user_id wich i need to perform a query on another table.
$gebruikerid = mysql_query("SELECT * FROM users where id LIKE '".$result['user_id']."'");
Now i want to use that value in a while loop
echo "<table><tr><th width=\"300\" align=\"left\" >Avatar</th><th width=\"300\" align=\"left\">Naam</th><th width=\"200\" align=\"left\">PSN Naam</th></tr>";
while($row2 = mysql_fetch_array($result))
{
echo "<tr><td><img height=\"50\" width=\"50\" src=\"/images/comprofiler/" . $row2['avatar'] . "\"></td><td>" . $gebruikernaam . "</td><td>" . $row2['cb_psnnaam'] . "</td></tr>";
}
echo "</table>";
I cannot get the query to read the values from the other table based on the id from the first table. Can someone help me?
In your case the best solution it's to use JOIN.
$result = mysql_query("SELECT comprofiler.*, users.* FROM comprofiler INNER JOIN users ON users.id = comprofiler.user_id WHERE cb_playstationgames LIKE '%FIFA%' ORDER BY comprofiler.id ASC");
You may have to manually specify the columns you want to select (after SELECT ) in case that you have same column names in both table and you need both.
$result = mysql_query("SELECT comprofiler.id as id_comprofiler, users.id as id_user, users.avatar ... " );
To answer your question why it does not work:
Second query does not use mysql_fetch_array.
$gebruikerid = mysql_fetch_array($gebruikerid);
It would be more secure & cleaner if you would use PDO or MySQLi prepared statements like:
$db = new \PDO(SEE PDO CUNSTRUCT)
$query1 = $db->prepare('
SELECT *
FROM comprofiler
WHERE cb_playstationgames LIKE :fifa
ORDER BY id ASC
');
$query2 = $db->prepare('
SELECT * FROM users where id LIKE :id
');
$query1->bindValue(':fifa', '%FIFA%', PDO::PARAM_STR);
$query1->execute();
while ($row = $query1->fetch(PDO::FETCH_ASSOC)) {
$query2->bindParam(':id', $row['user_id'], PDO::PARAM_INT);
$query2->execute();
//Holds associative array of second query
$row2 = $query->fetch(PDO::FETCH_ASSOC);
//$row1 holds associative array of first query
}
hello can i merge those 2 queries in one query my first query get the number of articles in database and second query get the sum of all visits of all article whats the best method to make it one query
$stmt = $db->query('SELECT * FROM stories');
$story_count = $stmt->rowCount();
$stmt = $db->query("SELECT sum(visits) FROM stories");
$total_visits = $stmt->fetchColumn();
Try like
$stmt = $db->query('SELECT COUNT(*) as total_cnt,
SUM(visits) as total_visits FROM stories');
then excute your query,you will get result from "total_cnt" and "total_visits"
SELECT COUNT(*) as total, SUM(visits) as total_visits FROM stories;
SELECT Story.*, COUNT(*) as total, SUM(Story.visits) as total_visits FROM stories AS Story;
If you want to get other fields along with SUM and COUNT use .*.
Yes try this:
$stmt = $db->query('SELECT count(*),sum(visits) FROM stories');
$result = $stmt->fetch_array(MYSQLI_NUM);
$story_count = $result[0];
$total_visits = $result[1];
How do I get the results and count the results in one query? (Im using PDO statements)
SELECT * FROM table; // gets results
SELECT COUNT(*) FROM table; // counts results
$result = mysql_query( "SELECT * FROM table");
$count = mysql_num_rows( $result);
Using PDO:
$statement = $dbh->prepare('SELECT * FROM table');
$statement->execute();
$count = $statement->rowCount();
This will put the record count at the end of each row.
SELECT *
, COUNT(1) OVER () AS RecordCount
FROM table;
SELECT *, (select count(*) FROM table) ct FROM table
you should execute only one query...
$sql = SELECT * FROM table;
$res = mysql_query($sql);
you can have total count by mysql_num_rows(); function.. like this ..
$count = mysql_num_rows($res);