Cara menggunakan convert pdo to mysqli

The server-side code at that link is insecure and has no error handling. It is also doing pagination in the most inefficient way, by selecting all the columns and all the rows of data just to get a count of the number of total rows. As already mentioned, the query to get the total number of rows should use SELECT COUNT(*) …

The main point of a prepared query is to separate the data from the sql syntax, so that any sql special characters in the data cannot break or alter the sql syntax, which is how sql injection is accomplished. Sql injection in a SELECT query would allow a hacker to append a UNION SELECT … query to your existing query to get and display the contents of any of your database tables, such as a user table. Do you really want someone to be able to see all of your user’s data?

A prepared query involves putting a simple ? place-holder into the sql query statement for each data value, preparing the query, then supplying the data values when the query is executed. When using wild-card match characters - % and _, these are added to the data values, not to the sql query statement.

Now the bad news. The mysqli prepared query programming interface is overly complicated and inconsistent, requiring you to learn two completely different usages for a non-papered and a prepared query. Even fetching data from a prepared query is totally different from what you are expecting. If you however switch to use the much simpler and more consistent PDO extension, you can treat the result from a non-prepared and a prepared query in exactly the same way and fetching data works like you would expect it to.

Cara menggunakan convert pdo to mysqli
Adem:

Is there an example that you can suggest?

Most of the code/examples posted on the web don’t directly get to the point, or are misusing things. Here’s a what pagination should be doing -

Pagination involves two SELECT queries.

The two queries need the same FROM/JOIN/WHERE/HAVING terms. If you build this common part of the query in a php variable, you can use it when building the two actual queries. You would also build the corresponding common prepared query input parameters in an array.

The first query is to get the number of matching rows. The number of matching rows and the number of rows per page setting are used to calculate the number of pages. The number of pages is used to test/limit the requested page number and is used when building the pagination links. This query uses SELECT COUNT(*), followed by the common part of the query.

The second query is to get the requested page of data. It would instead SELECT the list of columns you want, followed by the common part of the query, plus any ORDER BY term, and a LIMIT offset, row_count term. The offset value is calculated from the requested page number and the number of rows per page setting - (requested page number - 1) * number of rows per page. The row_count value is the number of rows per page setting. The offset and row_count values would be supplied via prepared query place-holders and would be added to the end of the array of prepared query input parameters.

All you have to do is execute those queries and fetch the data from them. If you are already using the mysqli extension, you should be able to do this without our help.

Note: a LIKE … comparison in a query, without any wild-card characters, is the same as an equal = comparison.

Halo sobat codekop.com, kali ini saya ingin membagi pengalaman di seri Datatables lagi yaitu cara membuat DataTables Serverside dengan PHP PDO & MySQL, dari kalian yang suka ngulik-ngulik website, khususnya pada backend development, kalian pasti cukup tau dengan DataTables, Ya Datatables merupakan library untuk menampilkan data dengan mengubah bentuk table, ke dalam fitur-fitur seperti search, filter, pagination, show perpage, sort by dan sebagainya.

Membuat tabel dengan library dataTables secara biasa tentu sudah sering kalian lakukan. Namun berbeda caranya jika dilakukan secara ServerSide dan pada php mysql.

Mengapa Harus Serverside ?

Karena jika data kalian masih sedikit yang harus load, maka biasanya gak ada masalah, tetapi jika data kalian yang sudah banyak, misal sudah ribuan, ratusan ribu atau jutaan, sering sekali mengalami kendala seperti tidak terload nya data, atau not responding pada browser karena banyak loop data yang di sajikan dalam 1 halaman. Hal ini membebankan server karena meload data terlalu lama, dan biasanya kadang data tersebut tidak terproses dengan baik.

Jadi solusi terbaik adalah datatables serverside processing.

Dengan melibatkan semua pemrosesan pada sisi server akan membuat sisi client menjadi ringan dan cepat. hal ini disebabkan data tidak diload secara keseluruhan dari database. Melainkan dilimit oleh sisi server sesuai dengan request yang dilkukan clientside. Dengan begitu, berapun jumlah record yang akan ditampilkan tidak ada lagi masalah terlihat, mengikuti request yang dilakukan clientside.

Sering saya pakai

Datatables serverside ini saya sering implementasikan pada beberapa project saya untuk menampung data-data yang besar dan kita tidak mungkin menampilkan semua data dari table yang jumlah ribuan, dikarenakan mempengaruhi performa web dan waktu eksekusi query.

Berikut ialah beberapa produksaya yang menggunakan dan mengimplementasi datatables serverside : https://www.codekop.com/subkat/premium  

BACA JUGA : Tutorial Cara membuat Datatables Serverside dengan CodeIgniter 4

Praktek :

1. Membuat Database MySQL

pertama-tama kita akan membuat database mysqlnya terlebih dahulu,  saya beri namanya dbnya tutorial_serverside atau copykan saya code dibawah ini simpan dengan nama dengan akhiran .sql lalu buat database pada phpmyadmin dan import :

-- phpMyAdmin SQL Dump
-- version 5.2.0
-- https://www.phpmyadmin.net/
--
-- Host: localhost
-- Generation Time: Aug 17, 2022 at 06:39 AM
-- Server version: 10.4.24-MariaDB
-- PHP Version: 8.1.6

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
START TRANSACTION;
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;

--
-- Database: `tutorial_serverside`
--

-- --------------------------------------------------------

--
-- Table structure for table `artikel`
--

CREATE TABLE `artikel` (
  `id_artikel` int(11) NOT NULL,
  `judul` varchar(255) NOT NULL,
  `kategori` varchar(255) NOT NULL,
  `penulis` varchar(255) NOT NULL,
  `tgl_posting` datetime NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

--
-- Dumping data for table `artikel`
--

INSERT INTO `artikel` (`id_artikel`, `judul`, `kategori`, `penulis`, `tgl_posting`) VALUES
(1, 'Pengenalan HTML', 'HTML', 'Joe Doe', '2020-01-11 02:06:12'),
(2, 'Pengenalan CSS', 'CSS', 'Joe Doe', '2020-01-11 00:00:00'),
(3, 'Pengenalan PHP', 'PHP', 'Joe Doe', '2020-01-11 02:06:12'),
(4, 'Pengenalan JavaScript', 'JS', 'Joe Doe', '2020-01-11 00:00:00'),
(5, 'Pengenalan MySQL', 'mysql', 'Joe Doe', '2020-01-11 00:00:00'),
(6, 'Advanced PHP', 'PHP', 'Joe Doe', '2020-01-11 00:00:00'),
(7, 'Pengenalan Vue JS', 'JS', 'Joe Doe', '2020-01-11 00:00:00'),
(8, 'Framework PHP', 'PHP', 'Joe Doe', '2020-01-11 00:00:00'),
(9, 'Basic CodeIgniter', 'PHP', 'Joe Doe', '2020-01-11 00:00:00');

-- --------------------------------------------------------

--
-- Table structure for table `kategori`
--

CREATE TABLE `kategori` (
  `id_kategori` int(11) NOT NULL,
  `nama_kategori` varchar(255) NOT NULL,
  `tgl_buat` datetime NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

--
-- Dumping data for table `kategori`
--

INSERT INTO `kategori` (`id_kategori`, `nama_kategori`, `tgl_buat`) VALUES
(1, 'Tutorial', '2020-01-12 00:00:00'),
(2, 'Teknologi', '2020-01-12 00:00:00'),
(3, 'Database', '2020-01-12 00:00:00'),
(4, 'Pemrograman', '2020-01-12 00:00:00'),
(5, 'JavaScript', '2020-01-12 00:00:00'),
(6, 'Framework', '2020-01-12 00:00:00');

-- --------------------------------------------------------

--
-- Table structure for table `subkat`
--

CREATE TABLE `subkat` (
  `id_subkat` int(11) NOT NULL,
  `subkat` varchar(255) NOT NULL,
  `id_kategori` int(11) NOT NULL,
  `tgl_add` datetime NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

--
-- Dumping data for table `subkat`
--

INSERT INTO `subkat` (`id_subkat`, `subkat`, `id_kategori`, `tgl_add`) VALUES
(1, 'HTML', 1, '2020-01-12 00:00:00'),
(2, 'CSS', 1, '2020-01-12 00:00:00'),
(3, 'PHP', 1, '2020-01-12 00:00:00'),
(4, 'JavaScript', 1, '2020-01-12 00:00:00'),
(5, 'Gojek', 2, '2020-01-12 00:00:00'),
(6, 'Grab', 2, '2020-01-12 00:00:00'),
(7, 'MySQL', 3, '2020-01-12 00:00:00'),
(8, 'MongoDB', 3, '2020-01-12 00:00:00'),
(9, 'PHP', 4, '2020-01-12 00:00:00'),
(10, 'Golang', 4, '2020-01-12 00:00:00'),
(11, 'Node JS', 4, '2020-01-12 00:00:00'),
(12, 'Phyton', 4, '2020-01-12 00:00:00'),
(13, 'Ruby', 4, '2020-01-12 00:00:00'),
(14, 'ASP', 4, '2020-01-12 00:00:00'),
(15, 'Vue JS', 5, '2020-01-12 00:00:00'),
(16, 'React JS', 5, '2020-01-12 00:00:00'),
(17, 'Angular JS', 5, '2020-01-12 00:00:00'),
(18, 'jQuery', 5, '2020-01-12 00:00:00'),
(19, 'CodeIgniter', 6, '2020-01-12 00:00:00'),
(20, 'Laravel', 6, '2020-01-12 00:00:00'),
(21, 'Symfony', 6, '2020-01-12 00:00:00'),
(22, 'Ruby On Rails', 6, '2020-01-12 00:00:00');

--
-- Indexes for dumped tables
--

--
-- Indexes for table `artikel`
--
ALTER TABLE `artikel`
  ADD PRIMARY KEY (`id_artikel`);

--
-- Indexes for table `kategori`
--
ALTER TABLE `kategori`
  ADD PRIMARY KEY (`id_kategori`);

--
-- Indexes for table `subkat`
--
ALTER TABLE `subkat`
  ADD PRIMARY KEY (`id_subkat`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `artikel`
--
ALTER TABLE `artikel`
  MODIFY `id_artikel` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=10;

--
-- AUTO_INCREMENT for table `kategori`
--
ALTER TABLE `kategori`
  MODIFY `id_kategori` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=7;

--
-- AUTO_INCREMENT for table `subkat`
--
ALTER TABLE `subkat`
  MODIFY `id_subkat` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=23;
COMMIT;

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

2. Membuat folder project dan file php 

pada langkah ini buat lah folder project kalian, kalau saya menamai serverside-php  dan mempunyai struktur folder dan file sebagai berikut :

 

Cara menggunakan convert pdo to mysqli
 

Keterangan :

  • index.php sebagai halaman utama berisi tentang tampilan utama data dari table yang kita tampilkan
  • data.php yaitu file yang menghasilkan format api json data berdasarkan query yang dikirimkan 
  • config/db.php yaitu file koneksi yang menghubungkan php ke database mysql
  • config/Datatables.php yaitu sebuah function builder yang digunakan untuk menggenerate hasil json khusus format api yang dibaca datatables berdasarkan kondisi sql yang di kirim

3. Koneksi php ke MySQL

pada langkah ini kita akan membuat script koneksi php ke mysql, ke folder file config/db.php , berikut ialah scriptnya :

Apa perbedaan penggunaan mysqli dan PDO?

PDO menggunakan pemrograman objek, mysqli extension tersedia dalam bentuk objek dan prosedural (diakses melalui fungsi-fungsi) sedangkan mysql extension sepenuhnya menggunakan pemograman prosedural.

Mengapa menggunakan PDO?

PDO bertujuan untuk membuat satu buah interface yang seragam untuk koneksi ke beragam jenis database. Ketika Anda menggunakan database yang didukung oleh PDO seperti misalnya MySQL, Oracle, MS.

Apa yang dimaksud dengan PDO?

"PDO - PHP Data Objects - adalah lapisan akses database yang menyediakan metode akses yang seragam ke beberapa database." Ini tidak memperhitungkan sintaks database-spesifik, namun memungkinkan proses peralihan database dan platform menjadi lebih mudah, cukup dengan mengganti string koneksi dalam banyak instance.

Apa yang dimaksud dengan Metode Data Access abstraction layer pada fitur koneksi PDO?

PDO bekerja dengan metode yang disebut “data-access abstraction layer”. Artinya, apapun jenis database server yang digunakan, kode PHP yang ditulis akan tetap sama. PDO menyediakan “abstraction layer” untuk berkomunikasi dengan database server.