0

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!

4
Contributors
7
Replies
10
Views
6 Years
Discussion Span
Last Post by klemme
0

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>";
0

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...

1

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.

Votes + Comments
Useful post
0

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);
0

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);
	
	
	
?>
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.