Upload presentasi
Presentasi sedang didownload. Silahkan tunggu
Diterbitkan olehRio Septiani Telah diubah "10 tahun yang lalu
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"]." "; echo "Alamat anda: ".$_POST["alamat"]." "; echo "Password anda: ".$_POST["password"]." "; echo "Jenis Kelamin anda: ".$_POST["gender"]." "; echo "Hobi anda adalah: ";
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 "; echo " logout "; }else { echo "Maaf anda tidak berhak mengakseshalaman ini !"; } ?>
7
Menulis File <? $filename="test.txt"; print "Writing to $filename "; $fp = fopen($filename,w) or die("Couldn't open $filename"); fwrite($fp,"Hello World \n"); fclose($fp); print "Appending to $filename "; $fp = fopen($filename,"a") or die("Couldn't open $filename"); fputs($fp,"And another thing\n"); fclose($fp); ?>
8
Deklarasi Class <? //defineclass.php class Person { private $name; function setName($name) { $this->name = $name; } function getName() { return $this->name; } }; ?> <? //defineclass.php $anton = new Person(); $anton->setName(“Anton"); $rasmus = new Person(); $rasmus->setName("Rasmus"); echo $erick->getName()."\n"; echo $rasmus->getName(); ?>
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
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!"); MENGHUBUNGKAN PHP DENGAN MySQL
16
// ------ putus hubungan dengan database mysql_close($link); // ------ buat tampilan tabel echo(" "); echo(" No Nama <td bgcolor=#CCCCCC>E-Mail Alamat <td bgcolor=#CCCCCC>Kota "); // ------ 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(" $nomor $nama <td bgcolor=#FFFFFF>$email $alamat <td bgcolor=#FFFFFF>$kota "); } echo(" "); ?>
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
26
NEXT
Presentasi serupa
© 2024 SlidePlayer.info Inc.
All rights reserved.