PHP and SQLIte: UPDATE statement fails

PHP and SQLIte: UPDATE statement fails

am 22.11.2007 09:18:58 von Luigi

Hello all!

I'm a newbie in PHP. I have written a short script that tries to
update a SQLite database with the user data. It is pretty simple,
something like this:


$sqlite = sqlite_open("mytest.db", 0666, $sqlite_error);

if(!$sqlite) {
die("Sqlite error: ".$sqlite_error);
}

$statement = "update myusers set mail=\"mymail\" where name=\"myuser
\"";
sqlite_query($sqlite, $statement);
sqlite_close($sqlite);

?>

It's just a test, but it doesn't work. It doesn't give me any error,
but the table is not updated. Can you suggest me a possible reason?

Thanks.

Re: PHP and SQLIte: UPDATE statement fails

am 22.11.2007 11:44:41 von oliver.graetz

Luigi schrieb:
> I'm a newbie in PHP. I have written a short script that tries to
> update a SQLite database with the user data. It is pretty simple,
> something like this:
> [...]
> It's just a test, but it doesn't work. It doesn't give me any error,
> but the table is not updated. Can you suggest me a possible reason?

I can't see where you created the table. Either it was never created or
it does not fit the query. But then, I cant't see what happened because
"it doesn't work" is a rather useless error description.

1. Please state what happen as detailed as possible. If PHP ouput
something, put into your message.
2. Give us code to reproduce your problem. Noone can reproduce your
error since you are not giving use the database structure.

And as the usual finishing words: Consider using PDO instead of
dedicated APIs like those for SQLite. Only one of many reasons: You
won't have to "relearn" another API once you decide to try out another
database...

OLLi

--
private String paula = "Brillant";
[Daily WTF]

Re: PHP and SQLIte: UPDATE statement fails

am 22.11.2007 14:29:53 von Jerry Stuckle

Luigi wrote:
> Hello all!
>
> I'm a newbie in PHP. I have written a short script that tries to
> update a SQLite database with the user data. It is pretty simple,
> something like this:
>
> >
> $sqlite = sqlite_open("mytest.db", 0666, $sqlite_error);
>
> if(!$sqlite) {
> die("Sqlite error: ".$sqlite_error);
> }
>
> $statement = "update myusers set mail=\"mymail\" where name=\"myuser
> \"";
> sqlite_query($sqlite, $statement);
> sqlite_close($sqlite);
>
> ?>
>
> It's just a test, but it doesn't work. It doesn't give me any error,
> but the table is not updated. Can you suggest me a possible reason?
>
> Thanks.
>

Luigi,

I don't use SQL Lite (I'm a MySQL fan, myself). But standard SQL uses
single quotes (') around string constants, not double quotes.

In your query, add a third variable to get any returned error code (as
in your sqlite_open() statement). If your sqlite_query() fails, print
the message in your error string.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

Re: PHP and SQLIte: UPDATE statement fails

am 22.11.2007 14:33:40 von Luigi

Hello OLLi,

the table and database are seen by the program, in fact I can
display easily the table content before I submit the update statement
(I've not reported all the code). The problem is not in the db/table
structure: the SELECT statement works, the UPDATE statement doesn't
work, in the sense that it seems completely ignored; PHP doesn't
output anything. I'll consider to use PDO.

Thanks a lot.

Re: PHP and SQLIte: UPDATE statement fails

am 22.11.2007 14:55:10 von oliver.graetz

Luigi schrieb:
> structure: the SELECT statement works, the UPDATE statement doesn't
> work, in the sense that it seems completely ignored; PHP doesn't
> output anything.

What really seems odd is the "doesn't output anything" part. Here's a
checklist:

- Do you have unfinished transactions in your code?
A missing COMMIT will rollback your db at the end.
- Is the sqlite file write protected (win32) or not
writable by your web server user (linux)?
- Did you correctly configure the error_reporting
for your PHP installation?
- echo() the update statement. Does this exact statement
work when performed manually with another sqlite tool?

OLLi

--
Guy: "Carpe diem." Girl: "I love the way french sounds."
[Lone Gunmen 12]

Re: PHP and SQLIte: UPDATE statement fails

am 22.11.2007 15:57:41 von Luigi

OK, I moved to PDO, but the problem still remains even if different as
I write more over here belove

> - Do you have unfinished transactions in your code?
> A missing COMMIT will rollback your db at the end.

There is, as you can see in the code below...

> - Is the sqlite file write protected (win32) or not
> writable by your web server user (linux)?

-rw-rw-rw-

> - Did you correctly configure the error_reporting
> for your PHP installation?

I don't know... this is the value I've got: 6143, but I have not idea
about what it means... I'm a newbie...

> - echo() the update statement. Does this exact statement
> work when performed manually with another sqlite tool?

Yes, it does perfectly...

Here is my full code:


try {
$dbh = new PDO('sqlite:../sqlite/myslite.db');

$statement = "update myusers set passwd='".md5('xxx')."' where
id='xxx'";
echo $statement, "\n";

$dbh->beginTransaction();
$dbh->execute($statement);
$dbh->commit();

$sth = $dbh->query('SELECT * from myusers');
foreach($sth as $row) {
echo $row['id'], "\n";
}
$dbh = null;
} catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "
";
die();
} catch (Exception $e) {
$dbh->rollBack();
echo "Failed: " . $e->getMessage();
}

?>

The problem is that now it prints out only $statement, and doesn't
prints the list of ids... if there were an error it would be printed,
but it isn't! Nonetheless something happens here:

$dbh->beginTransaction();
$dbh->execute($statement);
$dbh->commit();

In fact if I comment it, the id list is printed...

I'm very confused...

Thanks for your help...

Re: PHP and SQLIte: UPDATE statement fails

am 22.11.2007 20:06:51 von oliver.graetz

Luigi schrieb:
> OK, I moved to PDO, but the problem still remains even if different as
> I write more over here belove
>
>> - Do you have unfinished transactions in your code?
>> A missing COMMIT will rollback your db at the end.
>
> There is, as you can see in the code below...
>
>> - Is the sqlite file write protected (win32) or not
>> writable by your web server user (linux)?
>
> -rw-rw-rw-
>
>> - Did you correctly configure the error_reporting
>> for your PHP installation?
>
> I don't know... this is the value I've got: 6143, but I have not idea
> about what it means... I'm a newbie...

That's the numeric value of the sum of your error level constants:

http://de.php.net/error_reporting

It means E_ALL, so you should see all types of error messages.

> try {
> $dbh = new PDO('sqlite:../sqlite/myslite.db');

Typo alert "myslite" vs "mysqlite"?
Are you working on the wrong database?

> $statement = "update myusers set passwd='".md5('xxx')."' where
> id='xxx'";
> echo $statement, "\n";
>
> $dbh->beginTransaction();
> $dbh->execute($statement);

This MUST yield an error message since PDO has no execute method:

Fatal error: Call to undefined method PDO::execute() in C:\test.php on
line 9

> $dbh->commit();
>
> $sth = $dbh->query('SELECT * from myusers');
> foreach($sth as $row) {
> echo $row['id'], "\n";
> }
> $dbh = null;
> } catch (PDOException $e) {
> print "Error!: " . $e->getMessage() . "
";
> die();
> } catch (Exception $e) {
> $dbh->rollBack();

So you are only doing a rollback if the error is NOT a database error???

The script worked for me after changing to exec() and creating a mini
database.

OLLi

--
"No more Mr. Nice Gaius!!!"
[Baltar on Battlestar Galactica 107]

Re: PHP and SQLIte: UPDATE statement fails

am 22.11.2007 22:51:41 von nc

On Nov 22, 12:18 am, Luigi wrote:
>
> I'm a newbie in PHP. I have written a short script that tries to
> update a SQLite database with the user data. It is pretty simple,
> something like this:
>
> $sqlite = sqlite_open("mytest.db", 0666, $sqlite_error);
> if(!$sqlite) {
> die("Sqlite error: ".$sqlite_error);
> }
> $statement = "update myusers set mail=\"mymail\" where name=\"myuser\"";
> sqlite_query($sqlite, $statement);
> sqlite_close($sqlite);
>
> ?>
>
> It's just a test, but it doesn't work. It doesn't give me any error,
> but the table is not updated. Can you suggest me a possible reason?

For starters, you might want to replace double quotes in the query
with single quotes, just to make sure you are passing literals, rather
than identifiers:

http://www.sqlite.org/lang_keywords.html

Then, you need to check if execution of your query returned an error;
right now, you have this at the end of your script:

sqlite_query($sqlite, $statement);
sqlite_close($sqlite);

Change it to something like this:

$result = sqlite_query($sqlite, $statement, SQLITE_ASSOC,
$sqlite_error);
if ($result == false) {
die("Sqlite error: " . $sqlite_error);
}
sqlite_close($sqlite);

Cheers,
NC

Re: PHP and SQLIte: UPDATE statement fails

am 23.11.2007 09:24:12 von Luigi

> That's the numeric value of the sum of your error level constants:
>
> http://de.php.net/error_reporting
>
> It means E_ALL, so you should see all types of error messages.

Thanks

> Typo alert "myslite" vs "mysqlite"?
> Are you working on the wrong database?

No no, that's right....

> This MUST yield an error message since PDO has no execute method:
>
> Fatal error: Call to undefined method PDO::execute() in C:\test.php on
> line 9

OK, that was my fault! I've modified execute() with exec() and now
seems to work better. And yet I hadn't got errors!

> So you are only doing a rollback if the error is NOT a database error???

Well, it's only a test script... it was supposed to run without
errors...

> The script worked for me after changing to exec() and creating a mini
> database.

OK, thanks a lot...

Re: PHP and SQLIte: UPDATE statement fails

am 23.11.2007 10:22:38 von Luigi

Oh my God! I found the problem! It was the "../sqlite" directory... it
wasn't writable! The strange thing is that PHP (or SQLite, I don't
know) didn't yield me any error!

Thank you again for your help and suggestions!

Re: PHP and SQLIte: UPDATE statement fails

am 23.11.2007 17:52:24 von oliver.graetz

Luigi schrieb:
> Oh my God! I found the problem! It was the "../sqlite" directory... it
> wasn't writable! The strange thing is that PHP (or SQLite, I don't
> know) didn't yield me any error!

First of all:

> - Is the sqlite file write protected (win32) or not
> writable by your web server user (linux)?

People usually hate "told you so" speeches so I'll shut my mouth. Keep
in mind: If it's Linux and you experience problems, it will *ALWAYS* be
some problem with file or directory access rights. Many years of
experience have proven this to be true ;-)

About the error messages: The execute() method didn't exist on your
object so this is NOT a problem with SQLite not telling you about an
error because this is a very basic language level error in PHP. There
definitely IS something wrong with your error_reporting config. Find out
which php.ini you are using (can be found in the output of phpinfo())
and then make sure that you have display_error set to ON (only for your
development server, that is). Alternatively you can define a log file
where your error messages will be stored. Proper error reporting is as
important to successful programming as a pencil is to writing a letter.

Wish you a nice weekend. G'bye!

OLLi

--
Opposite?
[Patrick, Coupling]