Get database connection


(Ferdinand) #1

I access my database using activeRecords.
I want to make a simple query in one of my action methods in the controller class like this:

$con = ?????
    foreach ( $dataProvider->data as $row) {
         $sql = "DELETE FROM mytable WHERE Id = ".$row->id;
         if (mysqli_query($con,$sql) ) echo $row["Id"]." deleted.<br>";
    }

How can I get the current database connection?

Sorry for this beginner question. It’s a long time ago that I used yii …


(tri - Tommy Riboe) #2

Did you try “db” (default). Defined in config.

https://www.yiiframework.com/doc/guide/1.1/en/database.dao#establishing-database-connection


(Manuel Alejandro Gonzalez Prieto) #3

If you want to make a simple query, you should use this

https://www.yiiframework.com/doc/guide/2.0/en/db-dao#binding-parameters

Attention!!!
$sql = "DELETE FROM mytable WHERE Id = ".$row->id;

This query is very very danger!!!.. SQL Injection