Operasi - operasi dasar pada PHP MySQL.
1. Membuat koneksi
$hostmysql = “localhost”;
$username = “mysqlusername”;
$password = “mysqlpassword”;
$database = “namadatabase”;
$conn = mysql_connect(”$hostmysql”,”$username”,”$password”);
if (!$conn) die (”Koneksi gagal”);
mysql_select_db($database,$conn) or die (”Database tidak ditemukan”); >
Penjelasan Script:
a. mysql_connect
digunakan untuk membuat koneksi dari PHP ke server MySQL. Data mengenai hostname, mysql username, dan password yang digunakan telah diwakilkan oleh variabel $hostmysql, $username, $password. Penulisannya akan sama dengan:
mysql_connect(”localhost”,”username”,”password”);
b. mysql_select_db
untuk memilih database yang akan digunakan.
c. if (!$conn) die (”Koneksi gagal”);
jika koneksi gagal dibuat (!$conn), maka akan muncul pesan kesalahan
Setiap operasi PHP yang berhubungan dengan MySQL, akan membutuhkan sintaks diatas. agar lebih mudah, lebih baik disimpan terlebih dahulu dengan nama konfig.php. Jika sintaks tersebut dibutuhkan lagi, maka kita melakukan include terhadap file konfig.php tersebut.
2. Membuat tabel pada MySQL
include (”konfig.php”);
mysql_query(”CREATE TABLE user (
namadpnVARCHAR(20),
namablkg VARCHAR(20),
negara VARCHAR(20))”);
?>
Penjelasan script:
1. include (”konfig.php”);
perintah include digunakan untuk mengikut sertakan sebuah file (pada contoh diatas adalah file konfig.php).
2. mysql_query
format umum dari perintah ini adalah mysql_query(string dari query).
mysql_query akan sering dijumpai pada artikel kali ini.
3. Memasukkan data pada tabel
include (”konfig.php”);
$insert = “INSERT INTO users (namadpn,namablkg,negara)
VALUES (’Saya’,'Sendiri’,'Indonesia’)”;
mysql_query($insert) or die (”tidak dapat memasukkan data ke tabel”);
?>
4 . Menampilkan data dari tabel
include (”konfig.php”);
$query = “SELECT * FROM user”;
$result = mysql_query($query);
$numrows = mysql_num_rows($result);
while($row = mysql_fetch_array($result)){
echo “Jumlah data: $numrows
”;
echo “Nama Depan: $row[namadpn]
”;
echo “Nama Belakang: $row[namablkg]
”;
echo “Negara: $row[negara]“;
}
?>
Penjelasan script:
1. mysql_num_rows
digunakan untuk menghitung jumlah baris yang didapat dari hasil eksekusi query (mysql_query).
2. while ( ) {
}
digunakan untuk melakukan perulangan selama data yang yang diinginkan masih ada. (dalam contoh diatas: akan menampilkan semua isi dari table).
3. mysql_fetch_array
menampilkan data dari tabel dalam bentuk array
Untuk penggunaan lainnya (hapus, update, dsb) yang berubah hanya string query-nya saja.
Tampilkan postingan dengan label Database. Tampilkan semua postingan
Tampilkan postingan dengan label Database. Tampilkan semua postingan
Kamis, 19 Agustus 2010
PHP & MySQL: Tutorial Dasar
Connect to MySQL Database
Opening a connection to MySQL database from PHP is easy. Just use the mysql_connect() function like this
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'password';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
$dbname = 'petstore';
mysql_select_db($dbname);
?>
$dbhost is the name of MySQL server. When your webserver is on the same machine with the MySQL server you can use localhost or 127.0.0.1 as the value of $dbhost. The $dbuser and $dbpass are valid MySQL user name and password. For adding a user to MySQL visit this page : MySQL Tutorial
Don't forget to select a database using mysql_select_db() after connecting to mysql. If no database selected your query to select or update a table will not work.
Sometimes a web host will require you to specify the MySQL server name and port number. For example if the MySQL server name is db.php-mysql-tutorial.com and the port number is 3306 (the default port number for MySQL) then you you can modify the above code to :
$dbhost = 'db.php-mysql-tutorial.com:3306';
$dbuser = 'root';
$dbpass = 'password';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
$dbname = 'petstore';
mysql_select_db($dbname);
?>
It's a common practice to place the routine of opening a database connection in a separate file. Then everytime you want to open a connection just include the file. Usually the host, user, password and database name are also separated in a configuration file.
An example of config.php that stores the connection configuration and opendb.php that opens the connection are :
Source code : config.phps , opendb.phps
// This is an example of config.php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'password';
$dbname = 'phpcake';
?>
// This is an example opendb.php
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
mysql_select_db($dbname);
?>
So now you can open a connection to mysql like this :
include 'config.php';
include 'opendb.php';
// ... do something like insert or select, etc
?>
Closing the Connection
The connection opened in a script will be closed as soon as the execution of the script ends. But it's better if you close it explicitly by calling mysql_close() function. You could also put this function call in a file named closedb.php.
Source code : closedb.phps
// an example of closedb.php
// it does nothing but closing
// a mysql database connection
mysql_close($conn);
?>
Now that you have put the database configuration, opening and closing routines in separate files your PHP script that uses mysql would look something like this :
include 'config.php';
include 'opendb.php';
// ... do something like insert or select, etc
include 'closedb.php';
?>
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'password';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
$dbname = 'petstore';
mysql_select_db($dbname);
?>
$dbhost is the name of MySQL server. When your webserver is on the same machine with the MySQL server you can use localhost or 127.0.0.1 as the value of $dbhost. The $dbuser and $dbpass are valid MySQL user name and password. For adding a user to MySQL visit this page : MySQL Tutorial
Don't forget to select a database using mysql_select_db() after connecting to mysql. If no database selected your query to select or update a table will not work.
Sometimes a web host will require you to specify the MySQL server name and port number. For example if the MySQL server name is db.php-mysql-tutorial.com and the port number is 3306 (the default port number for MySQL) then you you can modify the above code to :
$dbhost = 'db.php-mysql-tutorial.com:3306';
$dbuser = 'root';
$dbpass = 'password';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
$dbname = 'petstore';
mysql_select_db($dbname);
?>
It's a common practice to place the routine of opening a database connection in a separate file. Then everytime you want to open a connection just include the file. Usually the host, user, password and database name are also separated in a configuration file.
An example of config.php that stores the connection configuration and opendb.php that opens the connection are :
Source code : config.phps , opendb.phps
// This is an example of config.php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'password';
$dbname = 'phpcake';
?>
// This is an example opendb.php
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
mysql_select_db($dbname);
?>
So now you can open a connection to mysql like this :
include 'config.php';
include 'opendb.php';
// ... do something like insert or select, etc
?>
Closing the Connection
The connection opened in a script will be closed as soon as the execution of the script ends. But it's better if you close it explicitly by calling mysql_close() function. You could also put this function call in a file named closedb.php.
Source code : closedb.phps
// an example of closedb.php
// it does nothing but closing
// a mysql database connection
mysql_close($conn);
?>
Now that you have put the database configuration, opening and closing routines in separate files your PHP script that uses mysql would look something like this :
include 'config.php';
include 'opendb.php';
// ... do something like insert or select, etc
include 'closedb.php';
?>
Kamis, 05 Agustus 2010
Cara Menghilangkan Password Database
Cara Menghilangkan Password Database,itulah topik yang akan saya bahas pada potingan kali ini
Dan berikut langkah-langkahnya:
Buatlah sebuah File Database Ms. Access dengan nama test.mdb dan password 123321, yang nantinya akan kita hilang password tersebut dengan menggunakan codingnya
* Tambah References Microsoft DAO 3.6 Object Library pada project Sobat
dan ketikan code di bawah ini pada form
Public Sub CLearDatabasePassword()
On Error GoTo Salah
Dim DtBase As Database
Set DtBase = OpenDatabase(App.path & "\Database\test.mdb", True, False, ";pwd=123321")
DtBase.NewPassword "123321", ""
DtBase.Close
Exit Sub
Salah:
If Err.Number <> 0 Then
MsgBox Err.Description & vbCrLf & Err.Source, , "Error"
End
End If
End Sub
*Penting file Ms. Access (test.mdd) harus satu folder dengan Aplikasi (Project)
Sekarang anda tinggal memanggil fungsi diatas pada form sintax nya seperti ini Call CLearDatabasePassword
maka database akan Hilang...
Salam dari Saya
Dan berikut langkah-langkahnya:
Buatlah sebuah File Database Ms. Access dengan nama test.mdb dan password 123321, yang nantinya akan kita hilang password tersebut dengan menggunakan codingnya
* Tambah References Microsoft DAO 3.6 Object Library pada project Sobat
dan ketikan code di bawah ini pada form
Public Sub CLearDatabasePassword()
On Error GoTo Salah
Dim DtBase As Database
Set DtBase = OpenDatabase(App.path & "\Database\test.mdb", True, False, ";pwd=123321")
DtBase.NewPassword "123321", ""
DtBase.Close
Exit Sub
Salah:
If Err.Number <> 0 Then
MsgBox Err.Description & vbCrLf & Err.Source, , "Error"
End
End If
End Sub
*Penting file Ms. Access (test.mdd) harus satu folder dengan Aplikasi (Project)
Sekarang anda tinggal memanggil fungsi diatas pada form sintax nya seperti ini Call CLearDatabasePassword
maka database akan Hilang...
Salam dari Saya
Langganan:
Postingan (Atom)


