• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 7178
  • Last Modified:

PHP/MySQL: How to fetch a single column result?

$query = "SELECT COUNT(*) FROM grouplists WHERE grouplists.gid='$groupid'";
$result2 = mysql_query($query);
$groupcounter = mysql_fetch_array($result2);

This would return an array, i dont want that. How can i fetch a single result (column)?

0
jtcy
Asked:
jtcy
  • 3
  • 2
  • 2
  • +3
5 Solutions
 
gamebitsCommented:
$query = "SELECT COUNT(columnname) FROM grouplists WHERE grouplists.gid='$groupid'";
0
 
jtcyAuthor Commented:
I mean I want to return the count number
0
 
gamebitsCommented:
So you would like to know the number of rows

$result = mysql_num_rows($query);

echo "$result";
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
jtcyAuthor Commented:
So i cant do smth like:

$query = "SELECT COUNT(*) FROM grouplists WHERE grouplists.gid='$groupid'";
$result = mysql_query($query);
$counter = XXX($result);

??
0
 
gamebitsCommented:
Or may be something like this (change the field name according to your database)

<?php
// Make a MySQL Connection

$query = "SELECT type, COUNT(name) FROM products GROUP BY type";
      
$result = mysql_query($query) or die(mysql_error());

// Print out result
while($row = mysql_fetch_array($result)){
      echo "There are ". $row['COUNT(name)'] ." ". $row['type'] ." items.";
      echo "<br />";
}
?>
0
 
TheAnarchistCommented:
Whatever happened to mysql_result() ?

<?php

// Make MySQL Connection - $conn is connection variable

$query = 'SELECT COUNT(*) FROM grouplists WHERE grouplists.gid="$groupid"';
$run_query = mysql_query ( $query , $conn ) or die ( mysql_error () );
$result = mysql_result ( $run_query , 0 ) or die ( mysql_error () );

echo 'There were ' . $result . ' rows returned from using count(*).';
exit;
?>

Find out more about mysql_result at http://www.php.net/mysql_result
0
 
vnsmanian2006Commented:
/*
Ur Problem!

$query = "SELECT COUNT(*) FROM grouplists WHERE grouplists.gid='$groupid'";
$result2 = mysql_query($query);
$groupcounter = mysql_fetch_array($result2);

This would return an array, i dont want that. How can i fetch a single result (column)?

*/

I guess that u dont need that records rather I assume u just need the record count (i.e. the number of records returned for the given sql);


U may try the following
$query = "SELECT COUNT(*) FROM grouplists WHERE grouplists.gid='$groupid'";
$result = mysql_query($query) or die ( mysql_error () ."SQL~".$query);
$record_count = mysql_num_rows($result);

If u think U may got more than 100 records for ur sql query and u want to know just the record count!Then U may try this , which is best for large records.

$query = "SELECT COUNT(id) FROM grouplists WHERE grouplists.gid='$groupid'";
// I assume id as the primary key for that given table,
// I use COUNT(id) ,which is little bit faster than count(*)
$result = mysql_query($query) or die ( mysql_error () ."SQL~".$query);
$record_count = mysql_result($result,0); // returns the 0th row
// The above statement returns the record count!
0
 
TheAnarchistCommented:
U may try the following
$query = "SELECT COUNT(*) FROM grouplists WHERE grouplists.gid='$groupid'";
$result = mysql_query($query) or die ( mysql_error () ."SQL~".$query);
$record_count = mysql_num_rows($result);

======================================================

Using mysql_num_rows() on a query that only selects count() is useless.

When you run a count() query in MySQL, it will always return ONE row - the contents of that row will be the amount returned from the count() function.

If you want to use count(), use mysql_result() with it.

Alternatively, if you insist on using mysql_num_rows(), you will have to get MySQL to return the rows themselves, not the count of them using count().
0
 
tkalchevCommented:
$query = "SELECT COUNT(*) FROM grouplists WHERE grouplists.gid='$groupid'";
$result2 = mysql_query($query);
list($groupcounter) = mysql_fetch_row($result2);
0
 
its_parikshitCommented:
Try this,

$query = "SELECT COUNT(*) as cnt FROM grouplists WHERE grouplists.gid='$groupid'";
$result2 = mysql_query($query);
$groupcounter = mysql_fetch_assoc($result2);

echo $groupcounter['cnt']; // <----  this has count
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 3
  • 2
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now