I am trying to get a count, using this query:
$count = $entityManager->getRepository('Model:Machine')
->createQueryBuilder('m')
->select('COUNT(m.id)')
->where('m.number = :number')
->setParameter('number', $value)
->getQuery()
->getSingleScalarResult();
If I use a $value that I know exists, then $count returns as 1.
If I use a value that I know does NOT exist, then $count is empty.
Why isn't it set to 0?
Thanks
getSingleScalarResult works different that you might think.
It expects to get a simple data from query and cast it to an integer.
When query couldn't get machines with number equal to parameter, there are no results, so results variable is empty.
Related
When I'm trying to get the number of rows on a SQL request and if not, the connection that follow fails.
But in any case (also if the request should return 0), it returns 1.
Here's my code :
$str = 'SELECT count(*) FROM admins WHERE mail = ? AND mdp = ?';
$arr = array($mail, $pass);
$rqt = sendRqt($str, $arr);
$tab = $rqt->fetchColumn();
$cnt = count($tab);
echo $cnt;
I don't understand why there's no time it returns 0
The problem is the use of the php function count().
You already have the correct number in your $tab variable as a string (probably, depends on php configuration / version) so you can echo it or cast it to an integer to make sure it is a number.
However, in php:
count(0) === 1
count('0') === 1
See here for example.
You should remove count($tab).
the SQL "COUNT(*)" allways returns a row with the count (quantity) of values, so if you apply the php count() function, always will return 1 because there is one row that contains the value of the COUNT sql function
I believe COUNT() needs a column name.WRONG!! count(*) should count all rows, but I still reccomend a column name like id or something. You could also use an AS to make life a little easier
$str = 'SELECT count(`COLUMNNAME`) AS cnt FROM table WHERE ....
I'm using the following code to find the number of rows returned:
global $wpdb;
$results = $wpdb->get_results("SELECT * FROM list WHERE queue = 1 ORDER BY id ASC LIMIT 0,1");
$rowcount = $results->num_rows;
echo $rowcount;
As you can see, the results are limited to 1 row, and when I run this query in SQL it returns 1 row just fine. But in PHP the $rowcount doesn't return any value at all.
Any ideas what might be wrong? I get no error.
I think you need to use count($results) since the method $wpdb->get_results returns a array of objects/arrays.
Another way to get the number of rows is to use $wpdb->num_rows. Apparently this works for
$wpdb->get_results.
From the docs (http://codex.wordpress.org/Class_Reference/wpdb), it says the following about get_results:
Generic, multiple row results can be pulled from the database with
get_results. The function returns the entire query result as an array.
Each element of this array corresponds to one row of the query result
and, like get_row, can be an object, an associative array, or a
numbered array. If no matching rows are found, or if there is a
database error, the return value will be an empty array. If your
$query string is empty, or you pass an invalid $output_type, NULL will
be returned.
The following snippet of SQL returns the results that I want for any number of entries until I insert a row with the value 0 for cumulative score. Once I do this I get no results returned. cumulativescore is an INT field with no constraints on it.
$stmt = $dbh->prepare("
select
cumulativescore
from
reporting
order by
cumulativescore asc
");
UPDATE
I think what's happening is that when I use the following code, fetchColumn returns 0 for the first column as it's in ascending order and this breaks my loop. By no results I meant $arr was empty but from this example I have learned that that wasn't the best way to test the query. How can I use fethchColumn and have 0 values? Or do I need to fetch as an associated array and get the value that way? As a sidenote, what's the most appropriate way to test PDO result sets for values/etc?
while($tmp = $stmt->fetchColumn()){
$arr[] = $tmp;
}
while(($tmp = $stmt->fetchColumn()) !== false){
$arr[] = $tmp;
}
three equal signs also check the type of the variable. This is true if the function returns not false exactly. If it returns 0 this is true.
I'm doing the following in a custom function:
$exists = $wpdb->query($wpdb->prepare('
SELECT COUNT(*)
FROM wp_%d_gdsr_data_article
WHERE post_id = %d
', $blog_id, $post_id));
$exists evaluates to 1 even if no rows are returned by the query. Also, var_dump($wpdb->queries) yields a NULL. Anyone know what is going on here?
thanks,
From the documentation:
The function returns an integer
corresponding to the number of rows
affected/selected. If there is a MySQL
error, the function will return FALSE.
(Note: since both 0 and FALSE can be
returned, make sure you use the
correct comparison operator: equality
== vs. identicality ===).
The query returns 1 row so the query() function returns 1 - and will always return 1 for the query that you post in your question, even if the number of rows selected by the COUNT is 0. Use get_var, get_row, or get_results as suggested by TheDeadMedic to get the actual result of the query, which might be 0, 1, 2, etc.
Use $wpdb->get_var($query) instead.
Accordingly, use $wpdb->get_row() to retrieve a single row as a single object (or array), and $wpdb->get_results() to get a result set.
Im trying to get the amount of elements in an array using the count() function, the result is a bit puzzling to me, considere the following example.
Assume that the user ID provided is wrong, and therefore had no matches, wouldnt then the result of the count function be 0? yet it is 1. can you explain to me why?
thanks
$q = mysql_query("select password from users where user_name = '" . $userID . "'");
$check = mysql_fetch_array($q);
$result = count($check);
echo "RESULT:" . $result;
That's the wrong way to count rows in a result set. First of all, from the documentation on mysql_fetch_array()
Returns an array of strings that
corresponds to the fetched row, or
FALSE if there are no more rows.
Ergo, if we do this
echo count( false );
We can see the output is 1. Why? Because the count() documentation tells us that as well
If var is not an array or an object
with implemented Countable interface,
1 will be returned.
To do a proper count, use mysql_num_rows()
$q = mysql_query("select password from users where user_name = '" . $userID . "'");
$result = mysql_num_rows( $q );
if ( 0 == $result )
{
// no rows in result
}
use mysql_num_rows instead of count. count returns 1 for the value FALSE
count() returns 1 because in case of failure $check is not an array.
From the documentation:
If var is not an array or an object with implemented Countable interface, 1 will be returned
But you should anyway handle error cases directly (if the query fails your code shouldn't even get to the point where you analize its results). Also as other have correctly said, that's not how you count the number of results a query returns as that's what mysql_num_rows() is for
mysql_fetch_array returns false if there are no more rows. count(false) = 1.
Try using mysql_num_rows if you want to count the number of results.