By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,143 Members | 1,919 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,143 IT Pros & Developers. It's quick & easy.

PHP and SQLIte: UPDATE statement fails

P: n/a
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:

<?php

$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.
Nov 22 '07 #1
Share this Question
Share on Google+
10 Replies


P: n/a
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]
Nov 22 '07 #2

P: n/a
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:

<?php

$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.
js*******@attglobal.net
==================

Nov 22 '07 #3

P: n/a
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.
Nov 22 '07 #4

P: n/a
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]
Nov 22 '07 #5

P: n/a
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:

<?php

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() . "<br/>";
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...
Nov 22 '07 #6

P: n/a
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() . "<br/>";
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]
Nov 22 '07 #7

P: n/a
NC
On Nov 22, 12:18 am, Luigi <luigipai...@libero.itwrote:
>
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
Nov 22 '07 #8

P: n/a
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...
Nov 23 '07 #9

P: n/a
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!
Nov 23 '07 #10

P: n/a
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]
Nov 23 '07 #11

This discussion thread is closed

Replies have been disabled for this discussion.