Q:

Mysql_error() Not Catching INSERT Error in PHP

new here and really green to programming, so go easy..
I discovered I have an INSERT that is failing because of a duplicate record error. I figured it out by running the query in a MySQL console with literals, where err#1062 popped up.
What I want to understand is why mysql_error() or mysql_errno() didn't catch this error in my PHP script. 
Below is a generic setup of what I've done. I have a form that submits to a php file that calls data_insert()
function data_insert($var1, $var2, $var3, $var4){

    $db = db_connect();
    $query = "INSERT INTO exampletable (id, id_2, id_3, id_4) 
            VALUES ('$var1', '$var2', '$var3', '$var4')";

    $result = $db->query($query);
        if (!$result) 
            { 
            echo ('Database Error:' . mysql_error());
            } 
        else 
            {
            echo "Data added to db";
            }
}

The DB connection:
function db_connect()
{
    $result = new MySQLi('localhost', 'root', 'root', 'dbname');
    if (!$result)
        throw new Exception('Could not connect to database server');
    else
        return $result;
}

Result I'm getting is:
Database Error:

PHP echos "Database Error:" because the INSERT fails, but no subsequent MySQL error info is echoed. Honestly, I'm not exactly sure what I'm supposed to see, but through reading some other SO questions, I've double-checked my php.ini file for error handling and E_ALL and display_errors is set appropriately (although not sure if it matters in this case).
Is there something in my logic that I'm not understanding, like the scope of the link resource mysql_error() takes? 
Thanks for your help, I'm hoping this is something embarrassingly obvious. 
I know the above is missing XSS and security precautions and uniform exception handling. Baby steps though. It's simplified here for discussion's sake. 

A:

You're using mysqli (note the i) for your DB operations, but are calling mysql_error (no i). They're two completely different interfaces, and do not share internal states at at all. DB handles/results from one are not usable in the other.
Try mysqli_error() instead (note the I).

