MySQLi extension Flashcards
What is the MySQLi extension?
MySQLi (MySQL Improved) is a PHP extension that provides an interface for accessing MySQL databases. It offers enhanced features over the older MySQL extension, including support for prepared statements, transactions, and improved security.
How many methods are available to connect to a database using MySQLi?
There are three methods to connect to a database using MySQLi:
Method 1 - MySQLi Object-Oriented
Method 2 - MySQLi Procedural
Method 3 - MySQLi with DSN
Method 1 - MySQLi Object-Oriented
$mysqli = new mysqli(“localhost”, “username”, “password”, “database”);
This method creates a new instance of the mysqli class, establishing a connection to the MySQL database using the provided credentials.
Method 2 - MySQLi Procedural
$connection = mysqli_connect(“localhost”, “username”, “password”, “database”);
This method uses the mysqli_connect() function to establish a connection to the MySQL database, returning a connection resource.
Method 3 - MySQLi with DSN
$dsn = “mysql:host=localhost;dbname=database”;
$username = “username”;
$password = “password”;
$mysqli = new mysqli($dsn, $username, $password);
This method uses a Data Source Name (DSN) to specify the database connection parameters, allowing for a more structured connection string.