tags:

views:

70

answers:

4

How can I grab the count value from the query MySQL query below using PHP.

Here is My MySQL code.

$dbc = mysqli_query($mysqli,"SELECT COUNT(*) FROM((SELECT users_friends.id
                            FROM users_friends
                            INNER JOIN users ON users_friends.user_id = users.user_id
                            WHERE users_friends.user_id = 1 
                            AND users_friends.friendship_status = '1')
                            UNION
                            (SELECT users_friends.id
                            FROM users_friends
                            INNER JOIN users ON users_friends.friend_id = users.user_id
                            WHERE users_friends.friend_id = 1
                            AND users_friends.friendship_status = '1')) as friends");
A: 
if ($result = mysqli_query($link, "SELECT Name FROM City LIMIT 10")) {
    printf("Select returned %d rows.\n", mysqli_num_rows($result));

    /* free result set */
    mysqli_free_result($result);

http://us.php.net/manual/en/mysqli.query.php

zod
+1  A: 

using SQL_CALC_FOUND_ROWS should simplify things:

$dbc = mysqli_query($mysqli,"SELECT SQL_CALC_FOUND_ROWS users_friends.id
                        FROM users_friends
                        INNER JOIN users ON users_friends.user_id = users.user_id
                        WHERE users_friends.user_id = 1 
                        AND users_friends.friendship_status = '1'
                       ");

then afterwards do

$rs = mysqli_query($mysqli, "SELECT FOUND_ROWS()"));
$rec = $rs->fetch_array();
$count = $rec[0];

This method will return the number of records that match the query, ignoring any LIMIT statement, whereas using $rs->num_rows will only give you the number of records actually returned. Depends which one you want.

bemace
Nice, I hadn't used this myself yet. Manual reference: http://dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_found-rows
deceze
Using SQL_CALC_FOUND_ROWS is inefficient. Unless you want to count the results AND fetch them, using the COUNT(*) function is much better.
Doug
@Doug if you have any info about efficiency of it I'd be interested (not being sarcastic), but he *is* counting and fetching in his example
bemace
The code on the answer does fetch the results, but the one in the original question does not. If the original intended result is just counting the records, then COUNT( * ) would be better. If you have to count and fetch the records, using SQL_CALC_FOUND_ROWS has better performance, because it does count and fetch the results in one operation. Using COUNT( * ) and then executing the query again to fetch the records is inneficient in this scenario.
Doug
A: 

Assuming that you are correctly connected to the MySQL server and your query are executed correctly, you can use the following code:

$values = mysql_fetch_row($dbc);
$count = $values[0];
Doug
A: 

Your query should look like SELECT COUNT(*) as numThings FROM xxx

The numThings is what you will reference in PHP:

$result = mysql_query("SELECT COUNT(*) as `numThings` FROM xxx");
$row = mysql_fetch_assoc($result);
$count = $row['numThings'];
gwagner