Minggu, 04 Mei 2014

BAB 27 PHP MySQL Update

BAB 27 PHP MySQL Update ________________________________________ The UPDATE statement is used to modify data in a database table. Syntax UPDATE table_name SET column_name = new_value WHERE column_name = some_value Note: SQL statements are not case sensitive. UPDATE is the same as update. To get PHP to execute the statement above we must use the mysql_query() function. This function is used to send a query or command to a MySQL connection. Example Earlier in the tutorial we created a table named "Person". Here is how it looks: FirstName LastName Age Peter Griffin 35 Glenn Quagmire 33 The following example updates some data in the "Person" table: After the update, the "Person" table will look like this: FirstName LastName Age Peter Griffin 36 Glenn Quagmire 33 ________________________________________ BAB 28 PHP MySQL Delete From ________________________________________ The DELETE FROM statement is used to delete records from a database table. Syntax DELETE FROM table_name WHERE column_name = some_value Note: SQL statements are not case sensitive. DELETE FROM is the same as delete from. To get PHP to execute the statement above we must use the mysql_query() function. This function is used to send a query or command to a MySQL connection. Example Earlier in the tutorial we created a table named "Person". Here is how it looks: FirstName LastName Age Peter Griffin 35 Glenn Quagmire 33 The following example deletes all the records in the "Person" table where LastName='Griffin': After the deletion, the table will look like this: FirstName LastName Age Glenn Quagmire 33 ________________________________________

BAB 26 PHP MySQL Order By Keyword

BAB 26 PHP MySQL Order By Keyword ________________________________________ The ORDER BY keyword is used to sort the data in a recordset. Syntax SELECT column_name(s) FROM table_name ORDER BY column_name Note: SQL statements are not case sensitive. ORDER BY is the same as order by. Example The following example selects all the data stored in the "Person" table, and sorts the result by the "Age" column: "; } mysql_close($con); ?> The output of the code above will be: Glenn Quagmire 33 Peter Griffin 35 ________________________________________ Sort Ascending or Descending If you use the ORDER BY keyword, the sort-order of the recordset is ascending by default (1 before 9 and "a" before "p"). Use the DESC keyword to specify a descending sort-order (9 before 1 and "p" before "a"): SELECT column_name(s) FROM table_name ORDER BY column_name DESC ________________________________________ Order by Two Columns It is possible to order by more than one column. When ordering by more than one column, the second column is only used if the values in the first column are identical: SELECT column_name(s) FROM table_name ORDER BY column_name1, column_name2 ________________________________________

BAB 25 PHP MySQL The Where Clause

BAB 25 PHP MySQL The Where Clause ________________________________________ The WHERE clause To select only data that matches a specific criteria, add a WHERE clause to the SELECT statement. Syntax SELECT column FROM table WHERE column operator value The following operators can be used with the WHERE clause: Operator Description = Equal != Not equal > Greater than < Less than >= Greater than or equal <= Less than or equal BETWEEN Between an inclusive range LIKE Search for a pattern Note: SQL statements are not case sensitive. WHERE is the same as where. To get PHP to execute the statement above we must use the mysql_query() function. This function is used to send a query or command to a MySQL connection. Example The following example will select all rows from the "Person" table, where FirstName='Peter': "; } ?> The output of the code above will be: Peter Griffin ________________________________________

BAB 24 PHP MySQL Select

BAB 24 PHP MySQL Select ________________________________________ Pernyataan SELECT yang digunakan untuk memilih data dari database. Syntax SELECT column_name(s) FROM table_name Catatan: SQL pernyataan tidak sensitif huruf. SELECT sama pilih. Untuk mendapatkan PHP untuk menjalankan pernyataan di atas kita harus menggunakan mysql_query () function. Fungsi ini digunakan untuk mengirim query atau perintah ke koneksi MySQL. Contoh: Contoh berikut memilih semua data yang tersimpan dalam "Person" table (The karakter * memilih semua data dalam tabel): "; } mysql_close($con); ?> Contoh di atas menyimpan data yang dikembalikan oleh mysql_query () fungsi dalam variabel $ hasil. Selanjutnya, kita menggunakan mysql_fetch_array () fungsi kembali ke baris pertama dari recordset sebagai array. Setiap panggilan ke mysql_fetch_array () kembali dengan baris berikutnya di recordset. Sementara yang loops lingkaran melalui semua catatan dalam recordset. Untuk mencetak nilai dari setiap baris, kita gunakan variabel PHP $ row ($ row [ 'firstname'] dan $ baris [ 'belakang']). Output dari kode di atas akan: Peter Griffin Glenn Quagmire ________________________________________ Display the Result in an HTML Table Contoh berikut memilih data yang sama seperti contoh di atas, tetapi akan menampilkan data dalam tabel HTML: Firstname Lastname "; while($row = mysql_fetch_array($result)) { echo ""; echo "" . echo $row['FirstName'] . ""; echo "" . echo $row['LastName'] . ""; echo ""; } echo ""; mysql_close($con); ?> The output of the code above will be: Firstname Lastname Glenn Quagmire Peter Griffin ________________________________________

Bab 23 PHP MySQL Insert Into

Bab 23 PHP MySQL Insert Into INSERT INTO pernyataan yang digunakan untuk memasukkan record baru ke dalam tabel database Insert Data Into a Database Table INSERT INTO table_name VALUES (value1, value2,....) atau INSERT INTO table_name (column1, column2,...) VALUES (value1, value2,....) Program25-1.php Insert Data From a Form Into a Database Program25-2.php
Firstname: Lastname: Age:
insert.php

Bab 22 Membuat Database dan Tabel

Bab 22 Membuat Database dan Tabel Membuat Database CREATE DATABASE database_name Program24-1.php Membuat Tabel CREATE TABLE table_name ( column_name1 data_type, column_name2 data_type, column_name3 data_type, ....... ) Program24-2.php Tipe Data di MySQL Numeric Data Types Description int(size) smallint(size) tinyint(size) mediumint(size) bigint(size) Integers terus saja. Maksimum jumlah digit dapat ditentukan dalam parameter ukuran decimal(size,d) double(size,d) float(size,d) Hold numbers with fractions. The maximum number of digits can be specified in the size parameter. The maximum number of digits to the right of the decimal is specified in the d parameter Textual Data Types Description char(size) Holds a fixed length string (can contain letters, numbers, and special characters). The fixed size is specified in parenthesis varchar(size) Holds a variable length string (can contain letters, numbers, and special characters). The maximum size is specified in parenthesis tinytext Holds a variable string with a maximum length of 255 characters text blob Holds a variable string with a maximum length of 65535 characters mediumtext mediumblob Holds a variable string with a maximum length of 16777215 characters longtext longblob Holds a variable string with a maximum length of 4294967295 characters Date Data Types Description date(yyyy-mm-dd) datetime(yyyy-mm-dd hh:mm:ss) timestamp(yyyymmddhhmmss) time(hh:mm:ss) Holds date and/or time Misc. Data Types Description enum(value1,value2,ect) ENUM is short for ENUMERATED list. Can store one of up to 65535 values listed within the ( ) brackets. If a value is inserted that is not in the list, a blank value will be inserted set SET is similar to ENUM. However, SET can have up to 64 list items and can store more than one choice Primary Keys dan Auto Increment Fields $sql = "CREATE TABLE Person ( personID int NOT NULL AUTO_INCREMENT, PRIMARY KEY(personID), FirstName varchar(15), LastName varchar(15), Age int )"; mysql_query($sql,$con);

Bab 21 Koneksi PHP MySQL

Bab 21 Koneksi PHP MySQL MySQL sangat sering digunakan bersama-sama dengan PHP. Koneksi ke database MySQL mysql_connect(servername,username,password); Parameter Description servername Optional. Specifies the server to connect to. Default value is "localhost:3306" username Optional. Specifies the username to log in with. Default value is the name of the user that owns the server process password Optional. Specifies the password to log in with. Default is "" Program23-1.php Menutup Koneksi Program23-2.php