473,387 Members | 1,550 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

PHP and SQLIte: UPDATE statement fails

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
10 7523
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
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Robert Mark Bram | last post by:
Hi All! I have the following two methods in an asp/jscript page - my problem is that without the update statement there is no error, but with the update statement I get the following error: ...
0
by: Fraser Hanson | last post by:
Hello, I have a table which has a foreign key relationship with itself. I want and expect my updates to cascade (deletes definitely cascade as expected) but instead I just get error 1217:...
8
by: pb648174 | last post by:
I have a single update statement that updates the same column multiple times in the same update statement. Basically i have a column that looks like .1.2.3.4. which are id references that need to...
5
by: Wing | last post by:
Hi all, I am writing a function that can change the value "Quantity" in the selected row of MS SQL table "shoppingCart", my code is showing below ...
1
by: amitbadgi | last post by:
HI i am getting the foll error while conv an asp application to asp.net Exception Details: System.Runtime.InteropServices.COMException: Syntax error in UPDATE statement. Source Error: Line...
6
by: FayeC | last post by:
I really need help figuring this out. i have a db with mostly text fields but 2. The user_id field is an autonumber (key) and the user_newsletter is a number (1 and 0) field meaning 1 yes the ...
19
by: Steve | last post by:
ASP error number 13 - Type mismatch with SELECT...FOR UPDATE statement I got ASP error number 13 when I use the SELECT...FOR UPDATE statement as below. However, if I use SELECT statement without...
0
by: gshawn3 | last post by:
Hi, I am having a hard time creating a Trigger to update an Oracle database. I am using a SQL Server 2005 Express database on a Win XP Pro SP2 desktop, linked to an Oracle 10g database on a...
2
by: travhale | last post by:
in a new project using .net 2005, c#. getting err message "Update requires a valid UpdateCommand when passed DataRow collection with modified rows." source RDBMS is oracle 8i. I add a new...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.