1 / 26

Pemrograman Internet Mobile

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>";

kathie
Download Presentation

Pemrograman Internet Mobile

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Pemrograman Internet Mobile Antonius R.C, S.Kom, M.Cs PHP 2

  2. Form

  3. Form (2)

  4. 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>";

  5. Register Global • Maka kita tidak bisa melewatkan variabel secara langsung • $_GET[“varname”] • $_POST[“varname”] • $_SESSION[“varname”] • $_COOKIE[“varname”] • $_REQUEST[“varname”] • $_FILES[“varname”]

  6. 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 !"; } ?>

  7. 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); ?>

  8. 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; } }; ?>

  9. 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

  10. 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");

  11. 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();

  12. 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);

  13. 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);

  14. 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.

  15. 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!");

  16. // ------ 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;

  17. // ------ 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>"); ?>

  18. 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");

  19. 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);

  20. 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);

  21. Contoh

  22. 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

  23. 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.

  24. Contoh

  25. Contoh

  26. NEXT

More Related