<?php
/**
 * Compatibilidad MySQLi para PHP 5.4+
 * Este archivo proporciona funciones de compatibilidad para reemplazar las funciones mysql_* obsoletas
 */

if (!defined('MYSQL_ASSOC')) {
    define('MYSQL_ASSOC', MYSQLI_ASSOC);
}
if (!defined('MYSQL_NUM')) {
    define('MYSQL_NUM', MYSQLI_NUM);
}
if (!defined('MYSQL_BOTH')) {
    define('MYSQL_BOTH', MYSQLI_BOTH);
}

// Variable global para mantener la conexión MySQLi
$mysqli_link = null;

if (!function_exists('mysql_connect')) {
    function mysql_connect($server, $username, $password, $new_link = false) {
        global $mysqli_link;
        $mysqli_link = new mysqli($server, $username, $password);
        if ($mysqli_link->connect_error) {
            return false;
        }
        return $mysqli_link;
    }
}

if (!function_exists('mysql_pconnect')) {
    function mysql_pconnect($server, $username, $password) {
        // mysqli no soporta conexiones persistentes de la misma manera
        return mysql_connect($server, $username, $password);
    }
}

if (!function_exists('mysql_select_db')) {
    function mysql_select_db($database_name, $link_identifier = null) {
        global $mysqli_link;
        $link = $link_identifier ? $link_identifier : $mysqli_link;
        return $link->select_db($database_name);
    }
}

if (!function_exists('mysql_query')) {
    function mysql_query($query, $link_identifier = null) {
        global $mysqli_link;
        $link = $link_identifier ? $link_identifier : $mysqli_link;
        return $link->query($query);
    }
}

if (!function_exists('mysql_fetch_array')) {
    function mysql_fetch_array($result, $result_type = MYSQLI_BOTH) {
        return $result->fetch_array($result_type);
    }
}

if (!function_exists('mysql_fetch_assoc')) {
    function mysql_fetch_assoc($result) {
        return $result->fetch_assoc();
    }
}

if (!function_exists('mysql_fetch_row')) {
    function mysql_fetch_row($result) {
        return $result->fetch_row();
    }
}

if (!function_exists('mysql_num_rows')) {
    function mysql_num_rows($result) {
        return $result->num_rows;
    }
}

if (!function_exists('mysql_num_fields')) {
    function mysql_num_fields($result) {
        return $result->field_count;
    }
}

if (!function_exists('mysql_fetch_field')) {
    function mysql_fetch_field($result, $field_offset = 0) {
        $result->field_seek($field_offset);
        return $result->fetch_field();
    }
}

if (!function_exists('mysql_free_result')) {
    function mysql_free_result($result) {
        return $result->free();
    }
}

if (!function_exists('mysql_close')) {
    function mysql_close($link_identifier = null) {
        global $mysqli_link;
        $link = $link_identifier ? $link_identifier : $mysqli_link;
        return $link->close();
    }
}

if (!function_exists('mysql_error')) {
    function mysql_error($link_identifier = null) {
        global $mysqli_link;
        $link = $link_identifier ? $link_identifier : $mysqli_link;
        return $link->error;
    }
}

if (!function_exists('mysql_real_escape_string')) {
    function mysql_real_escape_string($unescaped_string, $link_identifier = null) {
        global $mysqli_link;
        $link = $link_identifier ? $link_identifier : $mysqli_link;
        return $link->real_escape_string($unescaped_string);
    }
}

if (!function_exists('mysql_escape_string')) {
    function mysql_escape_string($unescaped_string) {
        return addslashes($unescaped_string);
    }
}
?>
