# [PATCH] sqlite: retrieve SQL syntax errors

- Thread: https://externals.io/message/18656
- Original archive: https://news-web.php.net/php.internals/18656
- Started by Nuno Lopes on 2005-09-02T18:15:11+00:00
- Messages: 8
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #18656 — Nuno Lopes — 2005-09-02T18:15:11+00:00

URL: https://externals.io/message/18656#18656

Hi,

I've made a simple patch to allow a user to retrieve the SQL syntax error 
occured in either sqlite_exec(), sqlite_query() or 
sqlite_unbuffered_query().
Untill now this wasn't possible, because sqlite_last_error() doesn't handle 
SQL syntax errors. The patch adds an additional parameter to those 3 
functions (OO and non-OO versions) to retrieve the error.

Patch: http://mega.ist.utl.pt/~ncpl/php_sqlite.txt
Example:
<?
$idx = sqlite_open(':memory:');
@sqlite_exec($idx, 'SELECT column', $error);
echo $error;
?>

I hope this can enter in PHP 5.1.0, as it is a fairly simple patch.


Nuno

P.S.: the patch also fixes a bug in the SQLiteDatabase constructor, where 
the $errorstr variable wasn't filled (because it wasn't beeing passed by 
reference).

---

## #18657 — Ilia Alshanetsky — 2005-09-02T18:23:02+00:00

URL: https://externals.io/message/18656#18657 · In reply to: #18656

I've just tried to replicate the problem and it appears to work fine:

$db = sqlite_open(":memory:");
sqlite_exec($db, "SELECT ***");
var_dump(sqlite_error_string(sqlite_last_error($db)));

prints "SQL logic error or missing database"

Ilia

Nuno Lopes wrote:
> Hi,
> 
> I've made a simple patch to allow a user to retrieve the SQL syntax
> error occured in either sqlite_exec(), sqlite_query() or
> sqlite_unbuffered_query().
> Untill now this wasn't possible, because sqlite_last_error() doesn't
> handle SQL syntax errors. The patch adds an additional parameter to
> those 3 functions (OO and non-OO versions) to retrieve the error.
> 
> Patch: http://mega.ist.utl.pt/~ncpl/php_sqlite.txt
> Example:
> <?
> $idx = sqlite_open(':memory:');
> @sqlite_exec($idx, 'SELECT column', $error);
> echo $error;
> ?>
> 
> I hope this can enter in PHP 5.1.0, as it is a fairly simple patch.
> 
> 
> Nuno
> 
> P.S.: the patch also fixes a bug in the SQLiteDatabase constructor,
> where the $errorstr variable wasn't filled (because it wasn't beeing
> passed by reference).

---

## #18658 — Nuno Lopes — 2005-09-02T18:35:11+00:00

URL: https://externals.io/message/18656#18658 · In reply to: #18657

OK, I probably didn't explain the problem well. So let me separate the 
problems:

the bug:
<?
$idx = new SQLiteDatabase(':memory:', 0, $errstr);
echo $errstr;
?>

prints:
Notice: Undefined variable: errstr in /cvs/php-src/sqlite.php on line 2
Notice: Undefined variable: errstr in /cvs/php-src/sqlite.php on line 3

-----

the "feature request":

<?
$db = sqlite_open(":memory:");
sqlite_exec($db, "SELECT column");
var_dump(sqlite_error_string(sqlite_last_error($db)));
?>

prints "SQL logic error or missing database", but you don't have a clue of 
what error is.
with my patch, you can retrieve the error (in the additional variable) you 
get in the Warning message ("no such column: column").


Nuno

----- Original Message ----- 
> I've just tried to replicate the problem and it appears to work fine:
>
> $db = sqlite_open(":memory:");
> sqlite_exec($db, "SELECT ***");
> var_dump(sqlite_error_string(sqlite_last_error($db)));
>
> prints "SQL logic error or missing database"
>
> Ilia
>
> Nuno Lopes wrote:
> > […]

---

## #18659 — Ilia Alshanetsky — 2005-09-02T19:11:45+00:00

URL: https://externals.io/message/18656#18659 · In reply to: #18658

> the "feature request":
> 
> <?
> $db = sqlite_open(":memory:");
> sqlite_exec($db, "SELECT column");
> var_dump(sqlite_error_string(sqlite_last_error($db)));
> ?>
> 
> prints "SQL logic error or missing database", but you don't have a clue
> of what error is.
> with my patch, you can retrieve the error (in the additional variable)
> you get in the Warning message ("no such column: column").

Aha, I see... that is indeed useful. +1

Ilia

---

## #18660 — Jani Taskinen — 2005-09-02T20:26:33+00:00

URL: https://externals.io/message/18656#18660 · In reply to: #18659

> > […]
>
> Aha, I see... that is indeed useful. +1

     Just commit it..

     --Jani

---

## #18724 — Nuno Lopes — 2005-09-08T19:57:34+00:00

URL: https://externals.io/message/18656#18724 · In reply to: #18659

> > […]
> 
> Aha, I see... that is indeed useful. +1
> 
> Ilia

I don't have php-src karma, so can you please commit the patch?

Thanks,
Nuno

P.S.: http://mega.ist.utl.pt/~ncpl/php_sqlite.txt

---

## #18726 — Ilia Alshanetsky — 2005-09-08T21:07:59+00:00

URL: https://externals.io/message/18656#18726 · In reply to: #18724

Applied to CVS, thanks for the patch and the reminder :-)

Ilia

---

## #18729 — Nuno Lopes — 2005-09-08T21:31:56+00:00

URL: https://externals.io/message/18656#18729 · In reply to: #18726

Thanks for reviewing and applying my patch ;)
Nuno


----- Original Message ----- 
> Applied to CVS, thanks for the patch and the reminder :-)
> 
> Ilia
