I’m trying to query my database with an array, I’m trying to count the number of rows returned from the query for each country in my database, and also count the rows for each of these countries with a digit equalling 1. Using the following code:
<?php
include ('mysqli_connect.php'); // indclude mysql connection functions
$countries = array('united states','canada','united kingdom');
foreach($countries as $country){
//e.g. $r_spain holds the results from the query below with spain as $country
$r_.$country = mysqli_query($dbc,"SELECT * FROM feelings WHERE country = '$country' AND digit='1'");
//loop through the results
while($row = mysqli_fetch_array($r_.$country, MYSQLI_ASSOC)){
$rowCount = mysqli_num_rows($r_.$country);
echo $country."=".$rowCount;
}
}
?>
This is the error message I get:
Catchable fatal error: Object of class
mysqli_result could not be converted
to string in
/home2/designwr/public_html/uwe/notalone/updates/percentage.php
on line 9
Can anybody point me in the right direction?
You are using a string concatenation for your variable name:
$r_.$countryis the result of the two strings added together.I would suggest using an array for the result set like: