Hello! I want to create a script which mails emails to subscribers. I'm almost done, but I'd like it to say "Sent to # people". I've tried using:

<?php

$dbc = mysqli_connect('localhost', 'username', 'password', 'database')
	or die('Error: Could not connect to database');

$query = mysql_query("SELECT * FROM table"); 
$number = mysql_num_rows($query); 
echo "Sent to ". $number;
	
	mysqli_close($dbc);

?>

When I try it I get:

Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in mysite.com/number.php on line 7

Any suggestions on how to fix it?

Thanks!

try using the COUNT() function in MySQL. Replace 'record_id' with the name of the column in your table.

$query = mysql_query("SELECT COUNT(record_id) AS counter FROM table");
$result = mysql_fetch_assoc($query);
echo "Sent to $result['counter'] People <br>";

try using the COUNT() function in MySQL. Replace 'record_id' with the name of the column in your table.

$query = mysql_query("SELECT COUNT(record_id) AS counter FROM table");
$result = mysql_fetch_assoc($query);
echo "Sent to $result['counter'] People <br>";

Doesn't seem to work...

Usually when you get that error message that you listed there is an error with the Query. When mysql_query returns a boolean instead of a result set it has caught some sort of error.

Echo out the mysql_error and and mysql_errno, nonshatter's query should have worked.

You can also try to run the query in phpadmin or some other MySql interface just to make sure it is valid.

commented: Useful post +4

It lloks like you are connecting to your DB using:
mysqli - With the i at the end..

And in your query you are using:
mysql - Withou the i at the end..

Could cause some problems im sure.

$dbc = mysqli_connect('localhost', 'username', 'password', 'database')	or die('Error: Could not connect to database'); 
$query = mysqli_query($dbc, "SELECT * FROM table");
// Maybe u need to write a where clause, where you specify which rows you need
// to count?
$rows = mysqli_num_rows($query);
// Or maybe use mysqli_affected_rows($dbc); 
echo 'Sent to ' . $rows . '';
mysqli_close($dbc);

Good Catch.

Actually, I found that the following code works:

<?php

$db = mysql_connect("hostname", "username", "password"); 
mysql_select_db("database",$db); 

$sql2 = "SELECT COUNT(id) FROM ".$db_prefix."table"; 
$result2 = mysql_query($sql2 ,$db); 
$row2 = mysql_fetch_row($result2); 
$num_of_items = $row2[0]; 

echo $num_of_items;
	
	mysql_close($db);
	
	
	
?>

Good, hope its working then!

Cheers

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.