// Connect to the database $mysqli = new mysqli("localhost", "username", "password", "database_name"); // Execute a SELECT query $query = "SELECT * FROM users WHERE username='john'"; $result = $mysqli->query($query); // Loop through the results while ($row = $result->fetch_assoc()) { echo $row['name'] . "\n"; } // Close the connection $mysqli->close();
// Connect to the database $mysqli = new mysqli("localhost", "username", "password", "database_name"); // Execute an INSERT query $query = "INSERT INTO users (name, email) VALUES (?, ?)"; $stmt = $mysqli->prepare($query); $stmt->bind_param("ss", $name, $email); $name = "John"; $email = "john@example.com"; $stmt->execute(); // Close the statement and connection $stmt->close(); $mysqli->close();This example connects to the database, prepares an INSERT statement, binds parameters to the statement, sets the parameter values, executes the statement and closes it, and then closes the connection. Package library: mysqli