Opens a MySQL database connection with host, account and database credentials.
The examples focus on the operation itself, including the important distinction: mysqli_real_connect completes a connection created with mysqli_init.
What It Does
Use the examples below to observe the behavior on a small dataset before extending it to application code.
Syntax and Return Value
mysqli_connect(...) — requires the corresponding optional extension
The displayed signature describes the API or identifies any correction or optional dependency. When a parameter is optional, supply it only when the task requires it.
Example 1: Basic Use
Read credentials from server-side configuration. Connection errors should be logged privately in production.
<?php
$link = mysqli_connect(getenv('DB_HOST') ?: '127.0.0.1', getenv('DB_USER') ?: 'app', getenv('DB_PASSWORD') ?: '', getenv('DB_NAME') ?: 'demo');
mysqli_set_charset($link, 'utf8mb4');
echo mysqli_get_host_info($link);
This example is meant for a configured application environment; output depends on the installed extension, external service, or database contents.
Example 2: Checking Behavior
Read credentials from server-side configuration. Connection errors should be logged privately in production. A connected handle provides server and charset metadata.
<?php
$link = mysqli_connect(getenv('DB_HOST') ?: '127.0.0.1', getenv('DB_USER') ?: 'app', getenv('DB_PASSWORD') ?: '', getenv('DB_NAME') ?: 'demo');
mysqli_set_charset($link, 'utf8mb4');
echo mysqli_get_server_info($link);
This example is meant for a configured application environment; output depends on the installed extension, external service, or database contents.
Example 3: Practical Example
Read credentials from server-side configuration. Connection errors should be logged privately in production. A connected handle provides server and charset metadata.
<?php
$link = mysqli_connect(getenv('DB_HOST') ?: '127.0.0.1', getenv('DB_USER') ?: 'app', getenv('DB_PASSWORD') ?: '', getenv('DB_NAME') ?: 'demo');
mysqli_set_charset($link, 'utf8mb4');
echo mysqli_character_set_name($link);
This example is meant for a configured application environment; output depends on the installed extension, external service, or database contents.
Important Behavior and Edge Cases
Avoid exposing passwords or connection errors to a public page; set the charset after connection.
This API is supplied by an optional extension that is not installed in the local test runtime, or requires a different PHP version. The example must be verified in a matching environment.
Comparison with Related Approaches
| Approach | Difference |
|---|---|
| mysqli_connect | Opens a MySQL database connection with host, account and database credentials. |
| Related API or alternative | mysqli_real_connect completes a connection created with mysqli_init. |
Common Beginner Mistakes
| Mistake | Correct approach |
|---|---|
| Ignoring the operation-specific limits | Avoid exposing passwords or connection errors to a public page; set the charset after connection. |
| Confusing it with a related API | mysqli_real_connect completes a connection created with mysqli_init. |
Key Takeaways
- Opens a MySQL database connection with host, account and database credentials.
- Avoid exposing passwords or connection errors to a public page; set the charset after connection.
- mysqli_real_connect completes a connection created with mysqli_init.
Use mysqli_connect when its documented behavior matches the problem. Test the relevant edge cases in your own runtime before deploying it.