$pdo = new \PDO('mysql:host=localhost;dbname=mydatabase', 'user', 'password'); $query = "SELECT * FROM users WHERE id = ?"; $stmt = $pdo->prepare($query); $stmt->execute([1]); $result = $stmt->fetchAll();
$mysqli = new \mysqli('localhost', 'user', 'password', 'mydatabase'); $query = "INSERT INTO orders (customer_name, order_date, total_amount) VALUES (?, ?, ?)"; $stmt = $mysqli->prepare($query); $stmt->bind_param('sss', $customerName, $orderDate, $totalAmount); $customerName = 'John Doe'; $orderDate = '2021-05-15'; $totalAmount = '100'; $stmt->execute();In this example, we connect to a database using MySQLi, prepare an insert query to add an order of a customer with a name, order date, and total amount, bind the parameters to the query using bind_param function, set the values of parameters and execute the query using execute function. The package library used in the examples is determined by the library used to establish a connection to the database and perform database operations. In the first example, we used PDO library, and in the second example, we used MySQLi library.