Selamat datang dan selamat belajar - Silahkan Follow lewat G+ agar anda tidak ketinggalan update materi kami - SALAM

Wednesday 2 December 2015

MySQL Connection



MySQL Connection menggunakan biner mysql:

Anda dapat membangun database MySQL menggunakan biner mysql pada command prompt.
Contoh:

Berikut ini adalah contoh sederhana untuk menghubungkan ke server MySQL dari command prompt:

[root @ host] # mysql -u root p
Enter Password:******

Anda akan mendapatkan command line seperti ini : 
mysql> dan anda bisa memasukkan perintah SQL nya di sini. 
Dan ini dia tampilannya :



Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2854760 to server version: 5.0.9

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

Dalam contoh di atas, kita telah menggunakan root sebagai pengguna tetapi Anda dapat menggunakan user lain. Setiap pengguna akan dapat melakukan semua operasi SQL.
Anda dapat memutuskan koneksi dari database MySQL setiap saat menggunakan perintah keluar di prompt mysql>.

mysql> exit
Bye

MySQL Connection menggunakan PHP Script:

PHP memberikan fungsi mysql_connect ()  untuk membuka koneksi database. Fungsi ini membutuhkan waktu lima parameter dan mengembalikan link identifier MySQL pada keberhasilan atau FALSE apabila gagal
Perintahnya:



connection mysql_connect(server,user,passwd,new_link,client_flag);


Parameter
Description
server
Optional - The host name running database server. If not specified, then default value is localhost:3036.
user
Optional - The username accessing the database. If not specified, then default is the name of the user that owns the server process.
passwd
Optional - The password of the user accessing the database. If not specified, then default is an empty password.
new_link
Optional - If a second call is made to mysql_connect() with the same arguments, no new connection will be established; instead, the identifier of the already opened connection will be returned.
client_flags
Optional - A combination of the following constants:
  • MYSQL_CLIENT_SSL - Use SSL encryption
  • MYSQL_CLIENT_COMPRESS - Use compression protocol
  • MYSQL_CLIENT_IGNORE_SPACE - Allow space after function names
  • MYSQL_CLIENT_INTERACTIVE - Allow interactive timeout seconds of inactivity before closing the connection

Anda dapat memutuskan koneksi dari database MySQL kapan saja dengan menggunakan fungsi PHP lain
mysql_close (). Fungsi ini mengambil parameter tunggal, yang merupakan koneksi dikembalikan oleh  
fungsi mysql_connect (). Perintahnya : bool mysql_close (sumber daya $ link_identifier); Jika sumber  tidak ditentukan maka database terakhir dibuka ditutup. Fungsi ini mengembalikan true jika menutup koneksi berhasil jika tidak maka kembali FALSE. Contoh: Coba contoh berikut untuk menghubungkan ke server MySQL:

<html>
<head>
<title>Connecting MySQL Server</title>
</head>
<body>
<?php
   $dbhost = 'localhost:3036';
   $dbuser = 'guest';
   $dbpass = 'guest123';
   $conn = mysql_connect($dbhost, $dbuser, $dbpass);
   if(! $conn )
   {
     die('Could not connect: ' . mysql_error());
   }
   echo 'Connected successfully';
   mysql_close($conn);
?>
</body>
</html>

No comments:

Post a Comment