custom query magento 2

Solutions on MaxInterview for custom query magento 2 by the best coders in the world

showing results for - "custom query magento 2"
Isabella
17 Jul 2018
1$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); // Instance of object manager
2$resource = $objectManager->get('Magento\Framework\App\ResourceConnection');
3$connection = $resource->getConnection();
4$tableName = $resource->getTableName('employee'); //gives table name with prefix
5
6//Select Data from table
7$sql = "Select * FROM " . $tableName;
8$result = $connection->fetchAll($sql); // gives associated array, table fields as key in array.
9
10//Delete Data from table
11$sql = "Delete FROM " . $tableName." Where emp_id = 10";
12$connection->query($sql);
13
14//Insert Data into table
15$sql = "Insert Into " . $tableName . " (emp_id, emp_name, emp_code, emp_salary) Values ('','XYZ','ABD20','50000')";
16$connection->query($sql);
17
18//Update Data into table
19$sql = "Update " . $tableName . "Set emp_salary = 20000 where emp_id = 12";
20$connection->query($sql);