Jump to content

Does anyone know if this is possible with MySQL and PHP...


suttercain

Recommended Posts

I currentley have to SQL statements to get the number of rows found (mysql_num_rows) like this..

 

<?php
$sql = mysql_query ("SELECT names FROM table WHERE name ='Frank'")
$count = mysql_num_rows($sql);
echo $count; //Works as desired.

$sql = mysql_query ("SELECT names FROM table WHERE name ='John'")
$count = mysql_num_rows($sql);
echo $count; //Works as desired.
?>

 

Can I somehow do a "row count" in another fashion and with a single mysql_query?

<?php
$sql = mysql_query ("SELECT names FROM table")
//somehow show number of John's found.
//somehow show number of Frank's found.
?>

 

Would in_array somehow come into play with what I am trying to achieve? Thanks.

 

SC

 

 

Link to comment
Share on other sites

Well you can do query's like this

 

SELECT names

  FROM table

WHERE name='Frank' or name='John'

 

You can then fetch the results into an Array or Object and sort it.

without the WHERE clause

lol :P idk why that seems stupid to me without a were clause but heh. Let MYSQL do as much work as possible :D

Link to comment
Share on other sites

SELECT names FROM table

 

Would load all names then use a array to sort.

Don't take this the wrong way, but this is a horrible suggestion.  You are going to get many more items than you need and force PHP to work way harder than it has to.  If the table is very large this approach can kill the site.

 

First off, as someone else suggested, doing a SELECT COUNT(*) .. is much more efficient than selecting everything and then calling mysql_num_rows().  If all you want is a count, do not ever select everything and then count the size of the result set; the reason you do not do this is because MySQL will have to send a potentially large result-set of data when all you want is a single number, very inefficient!

 

As for your particular question:

SELECT 
  COUNT(*) AS `n`, `name` 
FROM `table`
GROUP BY `name`
ORDER BY `name`

 

And if you only want specific names:

SELECT 
  COUNT(*) AS `n`, `name` 
FROM `table`
WHERE `name` IN ('Frank', 'Bob', 'Kelly')
GROUP BY `name`
ORDER BY `name`

Link to comment
Share on other sites

what I like to do is

<?php
$q = "select COUNT(*) from `Table` Where this='that'";
$r = mysql_query($q) or die(mysql_error());
$count = mysql_result($r,0);
?>

you can't do anything with the query without doing one more step, using mysql_num_rows or mysql_result are probably going to be faster than mysql_fetch_array(), but we are talking microseconds here

Link to comment
Share on other sites

<?php

$sql = mysql_query ("SELECT names FROM table WHERE name ='Frank'")

$count = mysql_num_rows($sql);

echo $count; //Works as desired.

 

$sql = mysql_query ("SELECT names FROM table WHERE name ='John'")

$count = mysql_num_rows($sql);

echo $count; //Works as desired.

?>

 

Can I somehow do a "row count" in another fashion and with a single mysql_query?

 

Code:

<?php

$sql = mysql_query ("SELECT names FROM table")

//somehow show number of John's found.

//somehow show number of Frank's found.

?>

 

----------------------------------------------------------------------------------

if you want to row count with one query just simply do in that way

 

$sql = mysql_query ("SELECT count(names) FROM table");

 

Simply put the column in the mysql count function and know the number of rows in any coulmn.

 

if still you are facing any problem than send me your question in detail.

thats it :)

 

 

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.