Langsung ke konten utama

Cara Mengetahui Error PHP Menggunakan Fungsi Mysql_error()

Saat kita telah selesai menulis kode php yang berhubungan dengan database mysql seperti memasukkan data, menampilkan data dan lain sebagainya kadang masih saja ada sintaks yang error. Tentu saja ini akan menyulitkan kita karena pesan error yang ditampilkan sulit dimengerti apalagi untuk pemula seperti saya. Pada artikel ini kita akan belajar bagaimana mendeteksi dan menangani error tersebut sehingga script php yang kita buat dapat berjalan dengan baik. Berdasarkan pengalaman yang telah saya alami, berikut ini adalah tips dan trik menangani sintaks error pada php yang berhubungan dengan database MySQL.

1. Error karena tidak bisa terhubung ke MySQLpesan error yang ditampilkan jika script kita tidak dapat terhubung dengan database adalah sebagai berikut:

Warning: mysql_query() [function.mysql-query]: Access denied for user 'ODBC'@'localhost' (using password: NO) in D:\xampp\htdocs\contoh\satu.php on line 6

atau

Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in D:\xampp\htdocs\contoh\satu.php on line 6

Biasanya pesan error seperti ini muncul karena kita lupa menuliskan script konfigurasi koneksi ke database mysql atau kita memasukkan konfigurasi yang salah. Nah, solusi untuk mengatasinya adalah dengan memasukkan script konfigurasi untuk koneksi mysql yang benar, contohnya sebagai berikut:

<?php
$host = "localhost"; //alamat host mysql
$user = "root"; //username mysql
$pass = ""; //password mysql ()
$db  = "nama_databasenya"; //nama database yang digunakan

$connect = mysql_connect($host,$user,$pass); //menghubungkan ke mysql
$sukses = mysql_select_db($db,$connect); //memilih database yang digunakan
?>

2. Error karena Kesalahan Query
Pesan error yang tampil jika kita salah menuliskan query diantaranya adalah sebagai berikut:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource inD:\xampp\htdocs\contoh\satu.php on line 8

atau

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource inD:\xampp\htdocs\contoh\satu.php on line 8

Saat awal-awal belajar php, tipe error seperti ini agak sulit diatasi karena pesan error yang tampil sulit dimengerti. tapi sebenarnya kita dapat dengan mudah mengatasinya dengan sedikit trik. caranya adalah dengan menuliskan echo "ada yang error: ".mysql_error();pada bagian setelah script yang dianggap error. perhatikan contoh berikut ini.


Nah, setelah kita menambahkan script echo "ada yang error: ".mysql_error(); maka pesan error menjadi lebih jelas sehingga kita bisa langsung menangani error tersebut. contonya pesan error akan tampil sebagai berikut:

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource inD:\xampp\htdocs\contoh\satu.php on line 8
ada yang error: Table 'truweb.tabelkaryawan' doesn't exist 

Demikian yang dapat saya sampaikan, semoga bermanfaat.

Komentar

Postingan populer dari blog ini

Use X++ wildcard (LIKE and NOT LIKE) in X++ select statement

For x++ select statements:  select firstOnly batchHistory      where batchHistory.Caption  LIKE  "*Test*"  For x++ queries:  queryBuildRange.value(*Test*); Note the LIKE instead of a '==' and the wildcards inside of the quotations. All other combinations of wildcard usage will work here. This is the same functionality as what the users would put in the grid filtering(eg. '*TEST*' in the caption field filter on the batch tasks form).  However, if you want to find all Captions that do not have the word Test in them (NOT LIKE, !LIKE), you will have to modify the above example slightly.  For x++ select statements:  select firstOnly batchHistory      where  !( batchHistory.Caption LIKE "*TEST*" ) ;  For x++ queries:  queryBuildRange.value(!*Test*);

Cleanup Jobs AX 2012

Cleanup jobs in Dynamics AX 2012 can help delete old data that is not needed anymore, and this reduction in database size can improve performance. In Dynamics AX 2012, cleanup jobs are available across various modules to run manually or in a batch mode. It is important to note that these jobs should be run after enough analysis determines that the business doesn’t need this data in the future. Please be sure to discuss with business management before deleting old data used in transactions that is not required any longer. Dynamics AX 2012 adds data to various tables for staging/intermediate purpose and this staging/intermediate data can be deleted once the transaction has been posted such as Sales update history, Purchase update history data. Here is the list of cleanup jobs available in Dynamics AX 2012 along with brief descriptions. All of these cleanup jobs are available in module > Periodic > Cleanup section Module Job Description General Ledger Clean up...

AX X++ str2Date

From time to time, a user will need to pull a date from a string. More often than not it is from an XML or from some other location. In order to actually use this date passed, it needs to be of data type 'date' so it can be written to fields, used in queries etc.  To do this, a user can use the 'str2Date' function. The str2Date function takes two parameters: datestring = The date in string format. It can be separated by either a dash ('-') or slash ('/') sequence = A three number combo using 1, 2, and 3 where 1 = day, 2 = month, and 3 = year. (e.g. 231 would be MM-YYY-DD, and 321 would be YYYY-MM-YY) If the sequence doesn't match the string or has an error, a '0' will result. Note that its very important to distinguish between day and month as an invalid month (>12) will result in a 0 and if both are <13, the date can translate totally wrong. I've detailed out the results of various scenarios below. Should be pretty self explan...