Powered By Blogger

Senin, 28 November 2011

VARIABEL, KONSTANTA & OPERATOR

Variabel
Variabel merupakan penampung sebuah data dan memiliki sifat datanya dapat berubah-ubah. Pada PHP semua variable dituliskan dengan tanda $ di belakang nama variable. Aturan penamaan variable sama dengan aturan penulisan variable pada bahasa lainnya yaitu :
1.     Diawali dengan tanda $. Sebenarnya tanda $ sendiri tidak termasuk nama dari variabel ini, tetapi hanya sekedar memberitahukan bahwa apa yang anda tulis adalah variabel. Jadi "$ini" adalah variabel, dan "ini" bukan variabel.
2.     Setelah tanda $, variabel harus diawali dengan huruf. Tidak boleh memulai variabel dengan angka, atau tanda baca lainnya. Sebagai contoh "$3tidakvalid" bukanlah penamaan variabel yang dibenarkan.
3.     Nama variabel hanya terdiri dari huruf, angka dan tanda garis bawah (_). Jadi nama seperti "$boleh_dong" dibenarkan, dan nama seperti "$apa_liat-liat" tidak dibenarkan.
4.     Tidak dibenarkan memberikan nama variabel dengan sesuatu (perintah, dll) yang sudah ada dan memiliki fungsi tertentu. Misalnya anda tidak dibenarkan menamakan variabel anda dengan "$print".
5.     Penamaan variabel pada PHP case sensitive, artinya huruf besar dibedakan dengan huruf kecil. Contohnya variabel "$ini" tidak sama dengan variabel "$iNi".

Materi Web 2

ELEMEN DASAR PHP
  • Karakter
  • Pengenal
  • Tipe data
  • Variabel
  • Konstanta
  • Operator
  1. Karakter : berupa huruf, sebuah angka tunggal, sebuah spasi, tanda control seperti carriage return (/r) atau simbol (+,&,?....)
  2. Pengenal(Identifier) : digunakan untuk memberi nama variabel, fungsi, atau kelas. Aturan penamaan kelas adalah sebagai berikut :
  • Karakter yang dapat digunakan adalah huruf, angka, atau garis bawah (_)
  • Karakter pertama harus berupa huruf atau garis bawah

Jumat, 18 November 2011

web generasi ke 2

WEB GENERASI 2.0 (WEB 2.0)
Perkembangan website saat ini sudah manjadi hal yang signifikan. Pertanyaannya adalah apakah website itu. Website adalah pada dasarnya sebuah cara untuk menampilkan data dan informasi melalui internet. Pemrograman website pada awalnya banyak menggunakan HTML (HyperText Markup Language) yang sering disebut bahasa tanda (tag HTML).
Namun seiring dengan waktu pemrograman website mulai berkembang dengan cepat. Saat ini web sudah mencapai generasi 2.0 (Web 2.0) dan bukanlah kata yang familiar bagi pengguna internet. Masih banyak pengguna yang mempertanyakan maksud dan manfaat dari penggunaan Web 2.0, terutama jika dibandingkan dengan web yang telah mereka kenal selama ini. Ketika Web 2.0 disebut sebagai tahap kedua dari perkembangan web yang telah ada saat ini, muncul kekhawatiran akan tidak kompatibelnya generasi web tersebut dengan program web browser yang dimiliki pengguna.

Senin, 07 November 2011

mata kuliah web 2

44 Fungsi PHP yang berhubungan dengan MySQL


1.      mysql_affected_rows

mysql_affected_rows  Get number of affected rows in previous MySQL operation

Description

int mysql_affected_rows ([ resource $link_identifier ] )
Get the number of affected rows by the last INSERT, UPDATE, REPLACE or DELETE query associated with link_identifier.

Parameters

link_identifier

The MySQL connection. If the link identifier is not specified, the last link opened by mysql_connect() is assumed. If no such link is found, it will try to create one as if mysql_connect() was called with no arguments. If no connection is found or established, an E_WARNING level error is generated.

Return Values

Returns the number of affected rows on success, and -1 if the last query failed.
If the last query was a DELETE query with no WHERE clause, all of the records will have been deleted from the table but this function will return zero with MySQL versions prior to 4.1.2.
When using UPDATE, MySQL will not update columns where the new value is the same as the old value. This creates the possibility thatmysql_affected_rows() may not actually equal the number of rows matched, only the number of rows that were literally affected by the query.
The REPLACE statement first deletes the record with the same primary key and then inserts the new record. This function returns the number of deleted records plus the number of inserted records.

Examples

Example #1 mysql_affected_rows() example
<?php
$link 
mysql_connect('localhost''mysql_user''mysql_password');
if (!
$link) {
    die(
'Could not connect: ' mysql_error());
}
mysql_select_db('mydb');

/* this should return the correct numbers of deleted records */
mysql_query('DELETE FROM mytable WHERE id < 10');
printf("Records deleted: %d\n"mysql_affected_rows());

/* with a where clause that is never true, it should return 0 */
mysql_query('DELETE FROM mytable WHERE 0');
printf("Records deleted: %d\n"mysql_affected_rows());
?>
The above example will output something similar to:
Records deleted: 10
Records deleted: 0

Example #2 mysql_affected_rows() example using transactions
<?php
$link 
mysql_connect('localhost''mysql_user''mysql_password');
if (!
$link) {
    die(
'Could not connect: ' mysql_error());
}
mysql_select_db('mydb');

/* Update records */
mysql_query("UPDATE mytable SET used=1 WHERE id < 10");
printf ("Updated records: %d\n"mysql_affected_rows());
mysql_query("COMMIT");
?>
The above example will output something similar to:
Updated Records: 10

 

Notes

Note: Transactions
If you are using transactions, you need to call mysql_affected_rows() after your INSERT, UPDATE, or DELETE query, not after the COMMIT.
Note: SELECT Statements
To retrieve the number of rows returned by a SELECT, it is possible to use mysql_num_rows().
Note: Cascaded Foreign Keys
mysql_affected_rows() does not count rows affected implicitly through the use of ON DELETE CASCADE and/or ON UPDATE CASCADE in foreign key constraints.
2.      mysql_close()
Untuk memutuskan hubungan dokumen dengan database MySQL.
syntax dasar
         mysql_close(connection);
contoh:
               <?
               $con = mysql_connect("localhost","root","12345");
               if(!$con)
               {
               die('Database tidak dapat terhubung: '.mysql_error());
               }
               // dan seterusnya
               mysql_close($con);
               ?>
3.      mysql_connect()
             Untuk menghubungkan dokumen dengan database MySQL.
syntax dasar
               mysql_connect(servername,username,password);
contoh:
               <?
               $con = mysql_connect("localhost","root","12345");
               if(!$con)
               {
               die('Database tidak dapat terhubung: '.mysql_error());
               }
               // dan seterusnya
               ?>

4.      mysql_create_db

mysql_create_db  Create a MySQL database

Description

bool mysql_create_db ( string $database_name [, resource $link_identifier ] )
mysql_create_db() attempts to create a new database on the server associated with the specified link identifier.

Parameters

database_name

The name of the database being created.
link_identifier
The MySQL connection. If the link identifier is not specified, the last link opened by mysql_connect() is assumed. If no such link is found, it will try to create one as if mysql_connect() was called with no arguments. If no connection is found or established, an E_WARNING level error is generated.

Return Values

Returns TRUE on success or FALSE on failure.
Examples
Example #1 mysql_create_db() alternative example
The function mysql_create_db() is deprecated. It is preferable to use mysql_query() to issue an sql CREATE DATABASE statement instead.
<?php
$link 
mysql_connect('localhost''mysql_user''mysql_password');
if (!
$link) {
    die(
'Could not connect: ' mysql_error());
}

$sql 'CREATE DATABASE my_db';
if (
mysql_query($sql$link)) {
    echo 
"Database my_db created successfully\n";
} else {
    echo 
'Error creating database: ' mysql_error() . "\n";
}
?>
The above example will output something similar to:
Database my_db created successfully
 

Notes

Note:

For backward compatibility, the following deprecated alias may be used: mysql_createdb()

5.      mysql_data_seek

mysql_data_seek  Move internal result pointer
Description
bool mysql_data_seek ( resource $result , int $row_number )
mysql_data_seek() moves the internal row pointer of the MySQL result associated with the specified result identifier to point to the specified row number. The next call to a MySQL fetch function, such as mysql_fetch_assoc(), would return that row.
row_number starts at 0. The row_number should be a value in the range from 0 to mysql_num_rows() - 1. However if the result set is empty (mysql_num_rows() == 0), a seek to 0 will fail with a E_WARNING and mysql_data_seek() will return FALSE.

Parameters

result
The result resource that is being evaluated. This result comes from a call to mysql_query().
row_number
The desired row number of the new result pointer.

Return Values

Returns TRUE on success or FALSE on failure.

Examples

Example #1 mysql_data_seek() example

<?php
$link 
mysql_connect('localhost''mysql_user''mysql_password');
if (!
$link) {
    die(
'Could not connect: ' mysql_error());
}
$db_selected mysql_select_db('sample_db');
if (!
$db_selected) {
    die(
'Could not select database: ' mysql_error());
}
$query 'SELECT last_name, first_name FROM friends';
$result mysql_query($query);
if (!
$result) {
    die(
'Query failed: ' mysql_error());
}
/* fetch rows in reverse order */
for ($i mysql_num_rows($result) - 1$i >= 0$i--) {
    if (!
mysql_data_seek($result$i)) {
        echo 
"Cannot seek to row $i: " mysql_error() . "\n";
        continue;
    }

    if (!(
$row mysql_fetch_assoc($result))) {
        continue;
    }

    echo 
$row['last_name'] . ' ' $row['first_name'] . "<br />\n";
}

mysql_free_result($result);
?>

Notes

Note:
The function mysql_data_seek() can be used in conjunction only with mysql_query(), not with mysql_unbuffered_query().

See Also

§  mysql_query() - Send a MySQL query
§  mysql_num_rows() - Get number of rows in result
§  mysql_fetch_row() - Get a result row as an enumerated array
§  mysql_fetch_assoc() - Fetch a result row as an associative array
§  mysql_fetch_array() - Fetch a result row as an associative array, a numeric array, or both
§  mysql_fetch_object() - Fetch a result row as an object


Example (Find Last Record):

<?php

$query
="SELECT * FROM `team` ORDER BY `team`.`id` ASC";
$result=mysql_query($query);
$last_row = mysql_num_rows($result) - 1;
if (
mysql_data_seek($result, $last_row)) { //Set Pointer To LAST ROW in TEAM table.

     
$row = mysql_fetch_row($result); //Get LAST RECORD in TEAM table
     
$id = $row[0] + 1; //New Team ID Value
     // more code here ...

} else { //Data Seek Error

    
echo "Cannot seek to row $last_row: " . mysql_error() . "\n";        

}

?>

** Note: 

1. The above example code relates to a MYSQL table named 'team' similar to this:

+-ID-+ TEAM NAME-+
| 49 |  Team 49  |
| 33 |  Team 33  |
| 84 |  Team 84  |
| 07 |  Team 07  |
+----+-----------+
** Records shown in the order inserted (i.e. TABLE NOT INDEXED)

2. In the above example code the field named ID is NOT set to AUTO-INCREMENT (i.e. field value set programmatically).

3. Wanting to insert a new team with a team number higher that the highest team number used, simply using the following code snippet

<?php

$query
="SELECT * FROM `team`";

?

resulted in an ERROR when using the mysql_data_seek() function  to select the record with the highest ID value.

4. Use of the ORDER statement in the query corrected the error/problem.

Here is a simple function to "peek" at the position of the internal pointer in a query result: 

<?php 
function mysql_pointer_position($result_set) { 
        
$num_rows = mysql_num_rows($result_set); 
        
$i = 0; 
        while(
$result = mysql_fetch_array($result_set)) { 
            
$i++; 
        } 
        
$pointer_position = $num_rows - $i; 

//Return pointer to original position 
        
if($pointer_position <= $num_rows - 1) { 
            
mysql_data_seek($result_set, $pointer_position); 
        } 
        return 
$pointer_position; 
    } 
?>