我正在尝试将一个现有的过程mysql查询用于oop查询。输入数据:
$select="visitor_country";
$startDate="2016-01-01";
$endDate="2016-12-31";
$groupBy="visitor_country";
require_once 'libraries/database.connect.php';
程序化mysql查询
$conAnalytics=@analyticsdbconnect::getSettings();
$countryQuery=sprintf("SELECT visitor_country,COUNT(*) as count FROM `analytics`.`analytics` WHERE date BETWEEN '2016-01-01' AND '2016-12-31'GROUP BY visitor_country ORDER BY count DESC");
$countrySqlQuery=mysqli_query($conAnalytics, $countryQuery)or die(mysqli_error());
$countryRow=mysqli_fetch_row($countrySqlQuery);
$countryRowCount=count($countryRow);
print_r($countryRow);
当前OOP查询
$query=$connection->prepare("SELECT ?, COUNT(*) as count FROM `analytics`.`analytics` WHERE date BETWEEN ? AND ? GROUP BY ? ORDER BY count");
$query->bind_param('ssss',$select,$startDate,$endDate,$select);
$query->execute();
$result=$query->get_result();
$row=$result->fetch_row();
print_r($row);
$query->close();
$connection->close();
这是一个mysql COUNT函数来检查用户按国家/地区访问。程序工作正常,因为我只有一个条目显示一个国家
Array ( [0] => India [1] => 1 )
。然而,一个人给我一些麻烦。每当我试图打印值时,我都会得到输出
Array ( [0] => visitor_country [1] => 2 )
as visitor_country是mysql列标题。
我试图创建一个类和函数,所以类似的查询可以通过一个函数处理并在谷歌图表中呈现。