This post is use for run sql query without using model in magento2. bY using this code we can get result execute query for create, read, update and delete.
// object manager Instance $object_Manager = \Magento\Framework\App\ObjectManager::getInstance(); $get_resource = $object_Manager->get('Magento\Framework\App\ResourceConnection'); $connection = $get_resource->getConnection(); // get connection // gives table name with prefix $your_table_name = $get_resource->getTableName('employee'); // add your table Name // Select Data from table in Magento2 $select_sql = "Select * FROM " . $your_table_name; $result_query = $db_connection->fetchAll($select_sql); foreach( $result_query as $key => $values ) { // and table fields as key in array. echo "key as fields => ".$key." \n "; // it will gives associated array echo "values of Query => ".$values." \n "; }
Magento2 : Custom Select Query
// Select Data from table in Magento2 $select_sql = "Select * FROM " . $your_table_name; $result_query = $db_connection->fetchAll($select_sql); foreach( $result_query as $key => $values ) { // and table fields as key in array. echo "key as fields => ".$key." \n "; // it will gives associated array echo "values of Query => ".$values." \n "; }
Magento2 : Custom Delete Query
// Delete Data from table in Magento2 $delete_sql = "Delete FROM " . $your_table_name." Where user_id = 116"; $db_connection->query($delete_sql);
Magento2 : Custom Insert Query
// Insert Data into table in Magento2 $insert_sql = "Insert Into " . $your_table_name . " (user_id,ser_name, ser_code, ser_salary) Values ('','TeSt','TeSt20','99999')"; $db_connection->query($insert_sql);
Magento2 : Custom Update Query
// Update Data into table in Magento2 $update_sql = "Update " . $your_table_name . "Set user_salary = 989898 where euser_id = 116"; $db_connection->query($update_sql);
Magento2 : All SQl Query
// object manager Instance $object_Manager = \Magento\Framework\App\ObjectManager::getInstance(); $get_resource = $object_Manager->get('Magento\Framework\App\ResourceConnection'); $connection = $get_resource->getConnection(); // get connection // gives table name with prefix $your_table_name = $get_resource->getTableName('employee'); // add your table Name // Select Data from table in Magento 2 $select_sql = "Select * FROM " . $your_table_name; $result_query = $db_connection->fetchAll($select_sql); foreach( $result_query as $key => $values ) { // and table fields as key in array. echo "key as fields => ".$key." \n "; // it will gives associated array echo "values of Query => ".$values." \n "; } // Delete Data from table in Magento 2 $delete_sql = "Delete FROM " . $your_table_name." Where user_id = 116"; $db_connection->query($delete_sql); // Insert Data into table in Magento 2 $insert_sql = "Insert Into " . $your_table_name . " (user_id,ser_name, ser_code, ser_salary) Values ('','TeSt','TeSt20','99999')"; $db_connection->query($insert_sql); // Update Data into table in Magento 2 $update_sql = "Update " . $your_table_name . "Set user_salary = 989898 where euser_id = 116"; $db_connection->query($update_sql);