Sabtu, 07 April 2012

CRUD OPERATION

STMIK AMIKOM Yogyakarta | Tohir Ismail | 2011
PHP & MySQL Cheat Sheet Session I
Practical E-Commerce
CRUD OPERATION


Database Connection
                <? php
                $ server = "localhost";
                $ username = "root";
                $ password = "";
                $ database = "student";
                mysql_connect ($ server, $ username, $ password) or die ("Connection Failed");
                mysql_select_db ($ database) or die ("Can not open database");
                ?>

Add Data
                $ result = mysql_query ("INSERT INTO table (field1, field2) VALUES ('isi1', 'isi2')");
                if (! $ result) {
                echo "Unable to add data";
                }

Reading Form (POST method)
                $ name = $ _POST ['name'];

Read the Get Request
                $ action = $ _GET ['command'];

Data Displays
                $ result = mysql_query ("SELECT * FROM table");
                while ($ data = mysql_fetch_array ($ result)) {
                echo $ data ['name'];
                echo $ data ['subject'];
                }

Update Data
                $ result = mysql_query ("UPDATE table SET field1 = 'isifield1', field2 = 'isifield2' WHERE id =           'idtoupdate'");
                if (! $ result) {
                echo "Failed to convert data";
                }

Remove Data
                $ result = mysql_query ("DELETE FROM table WHERE id = 'idtodelete'");
                if (! $ result) {
                echo "Failed to delete data";
                }


Search for Data
                $ result = mysql_query ("SELECT * FROM table WHERE id = 'idtosearch'");
                if (! $ result) {
                echo "The data sought was not found";
                }
                else {
                while ($ data = mysql_fetch_array ($ result)) {
                echo $ data ['name'];
                echo $ data ['subject'];
                }
                }

Filtering / Paging
                $ result = mysql_query ("SELECT * FROM tabelWHERE id = 'idtosearch' LIMIT 5") / / get the record of 5 beginning of record
                $ result = mysql_query ("SELECT * FROM tabelWHERE id = 'idtosearch' LIMIT 10 15") / / get the 15 record
start record No. 11



source : STMIK AMIKOM YOGYAKARTA

0 komentar

Posting Komentar

Statistik