Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql Yii2-连接到控制器操作内的数据库_Mysql_Database_Highcharts_Connection_Yii2 - Fatal编程技术网

Mysql Yii2-连接到控制器操作内的数据库

Mysql Yii2-连接到控制器操作内的数据库,mysql,database,highcharts,connection,yii2,Mysql,Database,Highcharts,Connection,Yii2,问候, Facts: Database named -> acastro Table called -> contacto Fields in table are -> id, nome, email 我正在制作一个Yii2应用程序,需要将highcharts图表连接到数据库中的表字段。 如何在名为actionAdmin的操作中连接到数据库,然后计算acastro数据库中存储的Contacto表中的id数。 在旧的Yii1.xx中,我使用以下方式建立连接: public fu

问候,

Facts:
Database named -> acastro
Table called -> contacto
Fields in table are -> id, nome, email
我正在制作一个Yii2应用程序,需要将highcharts图表连接到数据库中的表字段。 如何在名为actionAdmin的操作中连接到数据库,然后计算acastro数据库中存储的Contacto表中的id数。

在旧的Yii1.xx中,我使用以下方式建立连接:

public function actionAdmin() {
$sql = Yii::app()->db->createCommand('
SELECT count(*) as total
FROM contacto
')->queryAll();

$total = array();

for ($i = 0; $i < sizeof($sql); $i++){
$total[] = (int) $sql[$i]["total"];
}
$this->render('admin', array('total' => $total));
}
}
我做错了什么?有什么解决办法吗?
非常感谢。

我不太确定它是否能解决您的问题

但在yii2中,语法

使用app\models\Contacto//查看您的Contacto模型名称空间

$query = (new Query())->from('contacto');
$count = $query->count('column_name');

我希望这会有所帮助Yii2中最简单的语法是:

$count=(new \yii\db\Query)->from('TBL_NAME')->count('*');

它只是返回计数。例如:
500

非常感谢您的解决方案。两者都是正确的。:)
$count=(new \yii\db\Query)->from('TBL_NAME')->count('*');