I'm a beginner PHP programmer, so sorry if I'm asking some silly question. I've set up a MySQL database with a table carrying a list of boolean values. I want the PHP page count the number of 'true' on a column (from the table) and display it as a single number. Eg, if there is 4 'true's on the column, it display '4'. How do I do it?

I've tried to start it off, but it's displaying something wired - 'Resource id #4', which I have no idea what it is. Here is my code -

<?php
	include("conmysql.php");
    $selectdb = @mysql_select_db("vote");
    if (!$selectdb) die("Connect database fail!");
    
    //find number of people supporting
    $sql = 'SELECT COUNT(support) FROM address WHERE support=1';
	$result = mysql_query($sql);
	if (!result) {
		die('Invalid query: ' . mysql_error());
	}	
	echo "$result";
?>

CAN SOMEONE HELP!!

If you want to look at my real example, here is the page on my server - .

Thanks

Dani AI

Generated

The “Resource id #4” message happens because mysql_query() returns a result resource for SELECT statements — echoing that resource prints its internal identifier, not the data. The resource must be read with a fetch function (for example mysql_fetch_row() or mysql_fetch_assoc()) to get actual values. ()

The suggestion from (and repeated by ) to use mysql_num_rows() is misleading for this case. mysql_num_rows() returns how many rows the query returned (a COUNT() query returns exactly one row), so mysql_num_rows() would usually return 1 instead of the numeric count you want. The correct step is to fetch the single row and read the count column from it. ()

Quick minimal fix (keeps old ext/mysql but fixes the immediate bug):

<?php
// quick fix using old extension
$result = mysql_query("SELECT COUNT(*) AS cnt FROM my_table WHERE my_condition")
          or die(mysql_error());
$row = mysql_fetch_row($result);
echo (int)$row[0];
?>

Use this only as a short-term fix; the entire ext/mysql family is deprecated and was removed in PHP 7. Migrate to MySQLi or PDO and use prepared statements for safety and forward compatibility. Example (mysqli procedural, prepared):

<?php
$mysqli = new mysqli('localhost','user','pass','db');
$stmt = $mysqli->prepare("SELECT COUNT(*) FROM my_table WHERE flag = ?");
$stmt->bind_param('i', $flag);
$stmt->execute();
$stmt->bind_result($count);
$stmt->fetch();
echo $count;
$stmt->close();
$mysqli->close();
?>

For migration guidance and prepared-statement examples see the PHP manual. ()

Notes: if the column is stored as 0/1, SUM(column) can be an alternative to COUNT with WHERE. Always validate/cast the fetched value to an integer before displaying.

Recommended Answers

All 4 Replies

Member Avatar for Member #851298

Let's see if it works...

$count=mysql_num_rows($result);
if (!result) {
		die('Invalid query: ' . mysql_error());
	}	
echo $count; //not $result

I agree.

<?php include("conmysql.php");
    $selectdb = @mysql_select_db("vote");
    if (!$selectdb) die("Connect database fail!");
    //find number of people supporting
    $sql = 'SELECT COUNT(support) FROM address WHERE support=1';
	$result = mysql_query($sql);
        $count=mysql_num_rows($result);
        if (!result) {
	    die('Invalid query: ' . mysql_error());
	}	
    echo $count; //not $result
?>

Many Thanks!!! I've finally got this working!

Member Avatar for Member #851298

Cool :) So mark this thread as solved now!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.