How can we connect to a MySQL database from a PHP script?

  • Use the mysqli_connect() function
  • Use the mysql_connect() function
  • Use the pdo_connect() function
  • Use the db_connect() function
To connect to a MySQL database from a PHP script, you can use the mysqli_connect() function. This function establishes a connection to a MySQL database server using the provided credentials (host, username, password, and optional database name). It returns a MySQLi object that can be used to perform database operations such as executing queries and fetching results. It's important to note that the mysqli_connect() function is part of the MySQLi extension, which is the recommended extension for working with MySQL databases in PHP. The older mysql_connect() function is deprecated and should not be used in new code. Additionally, there is also the PDO extension that provides a consistent interface for connecting to various databases, including MySQL. You can use the PDO extension with the appropriate driver to connect to a MySQL database.
Add your answer
Loading...

Leave a comment

Your email address will not be published. Required fields are marked *