260 likes | 345 Views
Pemrograman Internet Mobile. Antonius R.C, S.Kom, M.Cs PHP 2. Form. Form (2). PHP nya?. if (empty($_POST["keterangan"])) $ket.="keterangan masih kosong"; if (isset($ket)){ echo "Error :".$ket; exit; } echo "Jadi nama anda: ".$_POST["nama"]."<br>";
E N D
Pemrograman Internet Mobile Antonius R.C, S.Kom, M.Cs PHP 2
PHP nya? if (empty($_POST["keterangan"])) $ket.="keterangan masih kosong"; if (isset($ket)){ echo "Error :".$ket; exit; } echo "Jadi nama anda: ".$_POST["nama"]."<br>"; echo "Alamat anda: ".$_POST["alamat"]."<br>"; echo "Password anda: ".$_POST["password"]."<br>"; echo "Jenis Kelamin anda: ".$_POST["gender"]."<br>"; echo "Hobi anda adalah: <br>";
Register Global • Maka kita tidak bisa melewatkan variabel secara langsung • $_GET[“varname”] • $_POST[“varname”] • $_SESSION[“varname”] • $_COOKIE[“varname”] • $_REQUEST[“varname”] • $_FILES[“varname”]
Session <? //berhasillogin.php session_start(); if (isset($_SESSION["user"])){ echo "Selamat datang ".$_SESSION["user"]." anda berhasil login<br>"; echo "<a href='logout.php'>logout</a>"; }else { echo "Maaf anda tidak berhak mengakseshalaman ini !"; } ?>
Menulis File <? $filename="test.txt"; print "Writing to $filename<br>"; $fp = fopen($filename,w) or die("Couldn't open $filename"); fwrite($fp,"Hello World \n"); fclose($fp); print "Appending to $filename<br>"; $fp = fopen($filename,"a") or die("Couldn't open $filename"); fputs($fp,"And another thing\n"); fclose($fp); ?>
Deklarasi Class <? //defineclass.php $anton = new Person(); $anton->setName(“Anton"); $rasmus = new Person(); $rasmus->setName("Rasmus"); echo $erick->getName()."\n"; echo $rasmus->getName(); ?> <? //defineclass.php class Person { private $name; function setName($name) { $this->name = $name; } function getName() { return $this->name; } }; ?>
Mysql_connect • mysql_connect(server, username, password) • connects to a MySQL server through a port • the default is the string "localhost:3306" • username is a string for the user name • password is a string for the password • returns FALSE on failure • Example • $db_link = mysql_connect("localhost:3306", "test", "123"); • there is also the persistent mysql_pconnect
Mysql_select_db • mysql_select_db(name, link) • select a database given by the string name • the link is optional and specifies the open link value such as $db_link returned by a connect statement. • if not supplied the last opened link is used. • returns TRUE on success else FALSE • Example • mysql_select_db("web_db");
Mysql_error() • mysql_error(link) • Return an error string or error number • the link is optional • if not supplied the last opened link is used. • Empty string is returned if there is no error. • Example • mysql_error();
mysql_query • mysql_query(query, link) • make a select query (link is optional) • query is a string for the MySQL query • Don't end the query with a semi-colon • Return value is a resource identifier or FALSE if • the query is SELECT, SHOW or DESCRIBE • Example (select all rows of books table) • $query = "SELECT * FROM books"; • $result = mysql_query($query);
Mysql_query • INSERT and UPDATE queries • for these queries a resource is not returned • TRUE is returned on success • FALSE is returned on failure • Example (describe the books table) • $query = "DESCRIBE books"; • $status = mysql_query($query);
MENGHUBUNGKAN PHP DENGAN MySQL • Agar script PHP yang kita buat dapat berhubungan dengan database dari MySQL dapat menggunakan fungsi berikut ini: • File utama.php: <?php function open_connection() { $host=”localhost”; $username=”root”; $password=””; $databasename=”privatdb”; $link=mysql_connect($host,$username,$password) or die ("Database tidak dapat dihubungkan!"); mysql_select_db($databasename,$link); return $link; } ?> • Isi dari variabel $host, $username, $password dan $databasename dapat disesuaikan sesuai dengan setting pada MySQL server yang ada.
MENGHUBUNGKAN PHP DENGAN MySQL • Contoh: Menampilkan data yang telah dibuat dengan menggunakan script PHP. • File contoh13.php: <?php // ----- ambil isi dari file utama.php require("utama.php"); // ----- hubungkan ke database $link=open_connection(); // ----- menentukan nama tabel $tablename="anggota"; // ----- perintah SQL dimasukkan ke dalam variable string $sqlstr="select * from $tablename"; // ------ jalankan perintah SQL $result = mysql_query ($sqlstr) or die ("Kesalahan pada perintah SQL!");
// ------ putus hubungan dengan database mysql_close($link); // ------ buat tampilan tabel echo("<table width=100% cellspacing=1 cellpadding=2 bgcolor=#000000>"); echo("<tr><td bgcolor=#CCCCCC>No</td><td bgcolor=#CCCCCC>Nama</td><td bgcolor=#CCCCCC>E-Mail</td><td bgcolor=#CCCCCC>Alamat</td><td bgcolor=#CCCCCC>Kota</td></tr>"); // ------ ambil isi masing-masing record while ($row = mysql_fetch_object ($result)) { // ----- mengambil isi setiap kolom $nomor=$row->nomor; $nama=$row->nama; $email=$row->email; $alamat=$row->alamat; $kota=$row->kota;
// ------ menampilkan di layar browser echo("<tr><td bgcolor=#FFFFFF>$nomor</td><td bgcolor=#FFFFFF>$nama</td><td bgcolor=#FFFFFF>$email</td><td bgcolor=#FFFFFF>$alamat</td><td bgcolor=#FFFFFF>$kota</td></tr>"); } echo("</table>"); ?>
Informasi kolom-kolom tabel • mysql_list_fields(database, table, link) • For a select query it retrieves information from given table in given database. link is optional • The returned resource can be used to obtain properties of the table such as names of the table columns and field type information • Example • $fields = mysql_list_fields("web_db","books");
Jumlah Fields • mysql_num_fields(result) • return the numbers of columns in a table • result is the resource returned by a call to the mysql_list_fields function • Example • $fields = mysql_list_fields("web_db", "books"); • $num_columns = mysql_num_fields($fields);
Field name • mysql_field_name(result, index) • return the name of the table column whose position is given by index (0,1,...) • result is the resource returned by a call to mysql_list_fields • Example: the first column name • $fields = mysql_list_fields("web_db", "books"); • $isbn = mysql_field_name($fields, 0);
Mysql_fetch_array • mysql_fetch_array(result) • combines mysql_fetch_row, mysql_fetch_assoc • returns row information as both an associative array and an indexed array
Beberapa fungsi lain • mysql_num_rows(result) • returns number of rows from a select query • result is the resource returned by the select query • mysql_affected_rows(result) • used after an INSERT, UPDATE, or DELETE query to return the number of rows affected • result is the resource returned • mysql_close(link) • close the database connection associated with the given link • doesn't do anything for persistent links.