Where can you obtain the $link parameter needed for mysqli_real_escape_string in PHP?

When using mysqli_real_escape_string in PHP to prevent SQL injection attacks, the $link parameter is required to establish a connection to the MySQL database. This parameter represents the MySQL connection object returned by mysqli_connect or mysqli_init. To obtain the $link parameter, you need to first establish a connection to the database using mysqli_connect or mysqli_init, and then pass this connection object as the $link parameter to mysqli_real_escape_string.

// Establish a connection to the MySQL database
$link = mysqli_connect("localhost", "username", "password", "database_name");

// Check connection
if (!$link) {
    die("Connection failed: " . mysqli_connect_error());
}

// Use mysqli_real_escape_string with the $link parameter
$unsafe_data = "unsafe data";
$safe_data = mysqli_real_escape_string($link, $unsafe_data);

// Close the connection
mysqli_close($link);