I'm trying to count the number of rows that are returned to $data in a wordpress database query that I'm making. See below
$data = $wpdb->get_results($wpdb->prepare("SELECT * FROM table WHERE wordpress_id=%d",get_current_user_id()),ARRAY_A);
The issue that I seem to be running into is the use of a prepared statement. I figure I could just write a loop to go through the array and tally them up but is there a more elegant way of doing this?
Many thanks in advance.
If you only need the count, change your SQL query to SELECT COUNT(*) as count .... This is efficient since the counting will be done on the database itself, and you won't be fetching all the data through the network.
If, however, you need both the data and the count, fetch the data as you are, and get the count by doing count($data).
Reference: count()
Related
I am trying to get some results for a query and count all the data results using PHP.
Actually I'm doing something like this:
$MQ=$cnx->query("SELECT COUNT(id) FROM table WHERE field=1;");
$MFA=$cnx->fetch_row();
$count=$MFA[0];
echo "Showing $count results";
$MQ=$cnx->query("SELECT id,name FROM table WHERE field=1;");
while($MFA=$MQ->fetch_assoc()){
// show something with $MFA[id] and $MFA[name]
}
I want to avoid using 2 queries, Is there any way to get the rows count and then get all the results without using an array?
You can use the num_rows property to get the number of rows in a result set.
$MQ=$cnx->query("SELECT id,name FROM table WHERE field=1;");
$count=$MQ->num_rows;
echo "Showing $count results";
while($MFA=$MQ->fetch_assoc()){
// show something with $MFA[id] and $MFA[name]
}
Is there any way to get the rows count and then get all the results without using an array?
There is, but you don't need it.
In fact, you should use an array, to separate your business logic from presentation logic.
And as long as you have an array, you can always count it's members.
You definitely should quit the practice of showing the query results right during the query execution. Instead, learn logic separation and use of templates.
$results_ts = mysqli_query($connection, "SELECT id, title, description, content
FROM prose WHERE title LIKE '%$search_title%' LIMIT 10");
if(isset ($_GET['search_title'])){
while($title_arr = mysqli_fetch_array($results_ts)){
echo $title_arr[id]; //and so on...
And that works as I wanted.
but when I try to add , count(*) after where content is, than while() loop echoes only one result, even when $title_arr[4](which stands for count) results in many.
Is it possible to do it this sort of way, or should I be running two separate queries?
You shouldn't do that in one query.
Think about the result you're expecting: when you do a count(*) query, what you get back is a result with only one row and one value. If you select multiple lines, then what should it do? Inject the count into each row of the data?
From a logical perspective, those are two different things you're looking for.
EDIT: counting the rows in your result after the fact is probably the best option, but that won't work if you're only looking for the first 10 entries, and there's no reason to SELECT the whole table if you don't need all the data. That's why count(*) is fulfilling such a different role.
COUNT() is an aggregate function so you can't use it like this. But if all you want is the number of rows this query would have returned you can use SQL_CALC_FOUND_ROWS to get the number of rows that would have been returned if thee was no LIMIT clause. You then can get the results by calling SELECT FOUND_ROWS() after your query is run.
Can someone explain how SQLite3 is used to give the number of rows found in a query?
mysql has mysql_num_rows and SQLite2 has a similar function but someone in SQLite3 development seems to have forgotten to add that function.
The examples that I have seen here and on other sites do not answer the question but instead only create more questions.
SO...
I have a query like $queryStr = $d->query("SELECT * FROM visitors WHERE uid='{$userid}' AND account='active';");
what I want to know is how do I find out how many results are in $queryStr
I am not looking for the number of rows in the database, just the number of rows in the "query results"
SQLite computes query results on the fly (because this is more efficient for an embedded database where there is no network communication).
Therefore, it is not possible to find out how many records a query returns without actually stepping through all those records.
You should, if at all possible, structure your algorithm so that you do not need to know the number of records before you have read them.
Otherwise, you have to execute a separate query that uses SELECT COUNT(*) to return the number of records, like this:
$countQuery = $d->query("SELECT COUNT(*) FROM visitors WHERE uid='{$userid}' AND account='active';");
In my program I launch an SQL query and get back a result resource. I then iterate through the rows of this result resource using the mysql_fetch_array() function and use the contents of the fields of each row to construct a further SQL query.
The result of launching this second query is the first set of results that I want. However, because the number of results produced by doing this is not many I want to make the search less specific by dropping the last record used to make the query.
e.g. the query which produces the first set of results I want could be:
SELECT uid FROM users WHERE (gender=male AND relationship_status=single
AND shoe_size=10)
I would then want to drop the last record so that my query became:
SELECT uid FROM users WHERE (gender=male AND relationship_status=single)
I have already written code to produce the first query but as I mentioned above I use the mysql_fetch_array function to iterate through ALL of the records. In subsequent "rounds" I only want to iterate through successively less records so that my query is less specific. How can I do this?
This seems like an very inefficient method too - so I'm welcome to any simple ideas which might make it more efficient.
EDIT: Thanks for the reply - Yeah I am actually doing this in my program. I am basically trying to implement a basic search algorithm by taking all the preferences a user has specified in the DB and using it to form a query to look for people with those preferences. So the first time search using all the criteria, then on successive attempts search using one less criteria and negate the user ids which were previously returned. At the moment I am constructing the query from scratch for each "round", but I want to find a way I can do this using the last query
Using the queries above, you could do:
SELECT uid
FROM users
WHERE uid NOT IN (
SELECT uid
FROM users
WHERE
(gender=male
AND relationship_status=single
AND shoe_size=10)
)
This will essentially turn your first query into a sub-query, and use that to negate the results returned. Ie, it will return all the rows, NOT IN the first query.
Can some shed some light on hoe to get the number of rows (using php) in a table without actually having to read all the rows? I am using squlite to log data periodically and need to know the table row count before I actually access specific data?
Apart from reading all rows and incrementing a counter, I cannot seem to work out how to do this quickly (it's a large database) rather simple requirement? I have tried the following php code but it only returns a boolean response rather that the actual number of rows?
$result = $db->query('SELECT count(*) FROM mdata');
Normally the SELECT statement will also return the data object (if there is any?)
Just use
LIMIT 1
That should work!! It Limits the result to ONLY look at 1 row!!
if you have record set then you get number or record by mysql_num_rows(#Record Set#);