How to store results of PDO/SQL Count in ModX Revo? - php

I need to store the results from an SQL COUNT query in a PHP variable.
I know ModX uses PDO to run its SQL queries but I am unsure of how to go about creating this.
So far I have tried:
$sql = "SELECT COUNT (*) FROM `table` WHERE x = $x";
$results = $modx->query($sql);
$count = mysql_fetch_array($results);
echo $count;
and:
$sql = "SELECT COUNT (*) FROM `table` WHERE x = $x";
$results = $modx->getCount($sql);
echo $results;
but with no results.
Would anyone know the correct way to do this in ModX (Revo) and PDO?

A couple different methods
$results = $modx->query("select `username` from modx_users");
if (!is_object($results)) {
return 'No result!';
}
else {
$r = $results->fetchAll(PDO::FETCH_ASSOC);
echo count($r);
print_r($r);
}
$results = $modx->query("select count(id) as uids from modx_users where id >= '1'");
if (!is_object($results)) {
return 'No result!';
}
else {
$r = $results->fetch(PDO::FETCH_ASSOC);
echo 'uids = '.$r['uids'];
}

Related

Get All Rows When Variable Empty PHP Mysql

$frame_type = '';
$ret = mysqli_query($con, "select * from products where status='1' AND frame_type = '$frame_type' ");
while ($row = mysqli_fetch_array($ret)) {
$emparray[] = $row;
}
Get All Rows If The $frame_type Is Empty I am trying this way but i get zero rows , How to fix that Where $frame_type has value then send to query else not
There are a some of things that is wrong with your question (code). But if you only want answer. Just copy this
$frame_type = '';
$query = "select * from products where status='1' ";
// strlen has value
if(strlen($frame_type)) {
$query .= "AND frame_type = '$frame_type'";
}
$ret = mysqli_query($con, $query);
while ($row = mysqli_fetch_array($ret)) {
$emparray[] = $row;
}
PS: It's never a safe idea to pass everything to the query. Use prepared statement if you can.

How to compare value in two tables from the same database

I have been trying to compare value in a table with another value in another table but only the else part is executing.
<?php
$sql = "SELECT * FROM user WHERE user_id=$row[user_id]";
$result = $conn -> query ($sql);
if ($result -> num_rows > 0) {
while ($row = $result ->fetch_assoc()) {
$sql1 = "SELECT * FROM jobpost WHERE jobpost_id=$_GET[id] ";
$result1 = $conn -> query ($sql1);
$row_count = mysqli_num_rows($result);
$row1_count = mysqli_num_rows($result1);
$remaining_rows = min($row_count, $row1_count);
$row = mysqli_fetch_assoc($result);
$row1 = mysqli_fetch_assoc($result1);
if($row["experience"] > $row1["experience"])
{
//some code to display something
echo "1";
}
else
{
echo "2";
}
} }
?>
Welcome to SO, it is hard to read your source code. You can use a simple query like this:
SELECT IF(u.experience > j.experience,1,0) FROM
(
(SELECT experience FROM user WHERE user_id = 1) u
JOIN
(SELECT experience FROM jobpost WHERE jobpost_id = 8) j
)
Try this query here: http://sqlfiddle.com/#!9/1742c0
Please check our datasource. Maybe the else case is correct.

How to get count of the sql search query?

How to get count of the sql search query
$u = new user();
$sql = "SELECT a.id FROM `accounts` AS a LEFT JOIN `users` AS u ON a.id = u.id WHERE a.id IS NOT NULL ";
$gender = $this->input->post('gender');
if($gender != NULL)
{
$sql .= "AND u.gender = '".$gender."' ";
}
$u->query($sql);
How to get count of the query results in $u->query($sql); .I need to set a validation on it. If the query results count is 0 , i need to set a message. Im using PHP Codeigniter ,datamapper library.
Thank you!!!
Just using count() function like this
...
$result = $u->query($sql);
$total_data = count($result);
if($u->exists())
{
echo "Found" // Do something
}
else
{
echo "Nothing found" //Do something
}
$result = $this->db->get();
For Codeigniter use $count = $result->num_rows(); // HERE IS YOUR COUNT
For OOP PHP use $count = $result->num_rows;

count the number of rows in a query

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;

Checking if mysql_query returned anything or not

$query = "SELECT * FROM `table`";
$results = mysql_query($query, $connection);
If 'table' has no rows. whats the easiest way to check for this.?
Jeremy Ruten's answer above is good and executes quickly; on the other hand, it only gives you the number of rows and nothing else (if you want the result data, you have to query the database again). What I use:
// only ask for the columns that interest you (SELECT * can slow down the query)
$query = "SELECT some_column, some_other_column, yet_another_column FROM `table`";
$results = mysql_query($query, $connection);
$numResults = mysql_num_rows($results);
if ($numResults > 0) {
// there are some results, retrieve them normally (e.g. with mysql_fetch_assoc())
} else {
// no data from query, react accordingly
}
You could use mysql_num_rows($results) to check if 0 rows were returned, or use this faster alternative:
$query = "SELECT COUNT(*) AS total FROM table";
$results = mysql_query($query, $connection);
$values = mysql_fetch_assoc($results);
$num_rows = $values['total'];
Alternatively you can simply check if the result of mysql_fetch_assoc is false.
$query = "SELECT * FROM `table`";
$results = mysql_query($query, $connection);
$Row = mysql_fetch_assoc($results);
if ($Row == false)
{
$Msg = 'Table is empty';
}
One thing i noticed that was missed was the fact that the query might not succeed, so you do need to check if the $results variable is set. I'll use the answer given by yjerem as an example.
$query = "SELECT COUNT(*) AS total FROM table";
$results = mysql_query($query, $connection);
if ($results) { // or use isset($results)
$values = mysql_fetch_assoc($results);
$num_rows = $values['total'];
}
If you loop through the results, you can have a counter and check that.
$x = 1;
$query = mysql_query("SELECT * FROM table");
while($row = mysql_fetch_assoc($query))
{
$x++;
}
if($x == 1)
{
//No rows
}

Categories