Working with Databases in PHP
Databases in PHP are used to store data and can be used to store user information, website content, and other types of data. PHP provides a set of functions for working with databases, such as connecting to a database, performing queries, and creating tables.
For example, a simple function that connects to a database:
function connectToDB($host, $user, $pass, $dbname) {
$conn = mysqli_connect($host, $user, $pass, $dbname);
return $conn;
}
In this example, the function connectToDB() is declared and takes four parameters, $host, $user, $pass, and $dbname. The function uses the mysqli_connect() function to connect to a database, and then returns the connection. The output of this function when called with the parameters “localhost”, “root”, “password”, and “my_db” would be a database connection object.