phpmysqlsqlinsertmysql-insert-id

mysql_insert_id() echos 0


I am not the first to have this problem but the solutions of the others don't work. This script always returns 0 for mysql_insert_id(); I am using multiple primary keys.

$sql = "INSERT INTO Produkte (Name,Price,Description,User)
VALUES ('".$_POST['name']."','".$_POST['price']."','".$_POST['description']."','".$_SESSION['user']."');";
$result = $GLOBALS['DB']->query($sql);
echo mysql_insert_id();
echo '<div class="saving">';
if($result){
echo "Saved!";
} else{
echo("Saving failed");
} echo '</div>';
} 

I already tried mysql_insert_id($link), where I linked $link to a mysql_connect() and mysql_insert_id($GLOBALS['DB']->MySQLiObj)

My $GLOBAL['DB']

if(!isset($GLOBALS['DB']))$DB = new \System\Database\MySQL(MYSQL_HOST,MYSQL_BENUTZER,MYSQL_KENNWORT,MYSQL_DATENBANK,MYSQL_PORT);

My MySQL class:

public $MySQLiObj = null;

function __construct($server, $user, $password, $db, $port = '3306')
{

    $this->MySQLiObj = new \mysqli($server, $user, $password, $db, $port);

    if (mysqli_connect_errno())
    {
        echo "Keine Verbindung zum MySQL-Server möglich.";
        trigger_error("MySQL-Connection-Error", E_USER_ERROR);
        die();
    }

    $this->query("SET NAMES utf8");
}

Solution

  • OK I finally made it! In my Class I added the function insert_id() and then added the insert_id variable locally.

    public function insert_id()
    {
        $result = $this->MySQLiObj->insert_id;
        return $result;
    }