Error updating AUTO INCREMENT

Asked

Viewed 41 times

2

I have the following code:

$sql='ALTER TABLE tributos_prod AUTO_INCREMENT=:ultimo_id';
try {
    $query_delete=$conecta->prepare($sql);
    $query_delete->bindValue('ultimo_id', $ultimo_idTP+1, PDO: : PARAM_STR);
    $query_delete->execute();
}
catch(PDOException $erro) {
    echo'[ERRO] ao atualizar AUTO-INCREMENTO: '.$erro->getMessage();
    exit;
}

The following error is displayed:

SQLSTATE[42000]: Syntax error or access Violation: 1064 You have an error in your SQL syntax; check the manual that Corresponds to your Mysql server version for the right syntax to use near ''13'' at line 1

I’ve already checked the variable value $ultimo_idTP, used intval() and it’s all right, but I’m not able to identify the problem.

  • The value of $ultimo_idTP would be 12? And why is the value passing as string for the SQL query? It should not be integer?

1 answer

2

Your parameter is being sent as a string because it generates the syntax error according to the message: syntax to use near ''13''.

To resolve this situation indicate the third argument as integer (PDO::PARAM_INT).

Change:

$query_delete->bindValue('ultimo_id', $ultimo_idTP+1, PDO::PARAM_STR);

To:

$query_delete->bindValue('ultimo_id', $ultimo_idTP+1, PDO::PARAM_INT);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.