PHP mysqli_savepoint() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
MySQLi

Definition

In PHP, mysqli_savepoint() creates a named marker inside the current transaction so you can roll back to that point later.

It does not commit or undo anything by itself. Use mysqli_rollback() with the same name to undo work after the marker, or mysqli_release_savepoint() to drop the marker while keeping the work.

Syntax

Call mysqli_savepoint() with the values you need. Start with this line.

PHP
$ok = mysqli_savepoint($mysql, 'sp1');
CallWhat it does
mysqli_savepoint($link, 'before_payment')Creates a savepoint named before_payment in the open transaction.
$mysqli->savepoint('sp1')Object-oriented form.
mysqli_rollback($link, 0, 'before_payment')Later undoes only the work after that savepoint.

Savepoints require an active transaction. Choose clear, unique names. Nested logical steps (validate → reserve → charge) often map cleanly to savepoints.

Library declarations
PHP
mysqli_savepoint(mysqli $mysql, string $name): bool

Parameters

ParameterDescription
mysqlRequired. Procedural style only: a mysqli connection.
nameRequired. Identifier for the new savepoint.

Return Value

Return ValueDescription
trueThe savepoint was created.
falseCreating the savepoint failed.

PHP Examples

Example 1 — Create a Savepoint

Fixed demo of beginning a transaction and setting a marker.

PHP
<?php
echo 'begin_transaction', PHP_EOL;
echo "savepoint('sp1') => true", PHP_EOL;

How It Works

  1. Open a transaction first.
  2. Call mysqli_savepoint($link, 'sp1').
  3. The name is just a string label on the server side.
  4. Sample output when connected to a demo database is a true return value.

Example 2 — Two Markers in One Transaction

Show how later steps can use different savepoint names.

PHP
<?php
foreach (['sp_a', 'sp_b'] as $name) {
    echo 'savepoint ', $name, PHP_EOL;
}

How It Works

  1. You can create more than one savepoint in the same transaction.
  2. Roll back to the name that matches the failure point.
  3. Releasing or rolling back earlier markers can invalidate later ones depending on server rules.
  4. Keep the naming scheme obvious in your code.

Example 3 — OO Call

Object style takes only the savepoint name.

PHP
<?php
class DemoMysqli {
    public function savepoint($name) {
        return $name !== '';
    }
}
$mysqli = new DemoMysqli();
echo $mysqli->savepoint('sp1') ? 'ok' : 'fail', PHP_EOL;

How It Works

  1. OO: $mysqli->savepoint('sp1').
  2. Procedural: mysqli_savepoint($link, 'sp1').
  3. Empty names are not useful markers.
  4. Combine with rollback() and release_savepoint() as needed.

Frequently Asked Questions

It sets a named marker in the current transaction for a later partial rollback or release.
No. It only places a marker. commit() or rollback() still decide the final outcome.
Call mysqli_rollback($link, 0, 'name') with the same name.
Call mysqli_release_savepoint() with that name.
Yes. Create the savepoint only after begin_transaction() (or an equivalent open transaction).

Next: mysqli_select_db()

mysqli_select_db() chooses the default database for the open MySQL connection.

mysqli_select_db() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful