• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 7199
  • 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

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