31 Oct 2013

Magento: How To Select, Insert, Update, And Delete Data?

Suppose, I have a database table named ‘test‘ with the following fields:-
id : int, auto increment, primary key
title : varchar
content : text
status : 1
Suppose, I have a module named ‘mytest‘. Here follows the code to select, insert, update, and delete data from the ‘test‘ table.



INSERT DATA :
$data contains array of data to be inserted. The key of the array should be the database table’s field name and the value should be the value to be inserted.



or In Simple way you can do means check the below code :




SELECT DATA :





Similarly, to print content, we need to write $item->getContent().



or In Simple way you can do means check the below code :



you can check which method is available in this $connection object by simple way:



UPDATE DATA :
$id is the database table row id to be updated.
$data contains array of data to be updated. The key of the array should be the database table’s field name and the value should be the value to be updated.




Or
For only single data value by id lets say I want to update contnet value ‘test’ to ’newtestcontent’;



or In Simple way you can do means check the below code :




DELETE DATA :

$id is the database table row id to be deleted.




or In Simple way you can do means check the below code :