473,545 Members | 1,471 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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("my test.db", 0666, $sqlite_error);

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

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

?>

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 7542
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("my test.db", 0666, $sqlite_error);

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

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

?>

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*******@attgl obal.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->beginTransacti on();
$dbh->execute($state ment);
$dbh->commit();

$sth = $dbh->query('SELEC T * 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->beginTransacti on();
$dbh->execute($state ment);
$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->beginTransacti on();
$dbh->execute($state ment);
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('SELEC T * 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...@li bero.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("my test.db", 0666, $sqlite_error);
if(!$sqlite) {
die("Sqlite error: ".$sqlite_error );
}
$statement = "update myusers set mail=\"mymail\" where name=\"myuser\" ";
sqlite_query($s qlite, $statement);
sqlite_close($s qlite);

?>

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($s qlite, $statement);
sqlite_close($s qlite);

Change it to something like this:

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

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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
6216
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: Error Type: Microsoft OLE DB Provider for ODBC Drivers (0x80004005) Syntax error in UPDATE statement. /polyprint/dataEntry.asp, line 158
0
5889
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: foriegn key error. I have written example code to use in reproducing the problem: # Create the table
8
11585
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 be updated when a group of items is copied. I can successfully do this with cursors, but am experimenting with a way to do it with a single update...
5
9822
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 ------------------------------------------------------ static void addQuantity(int rowNum, int oldQuantity) { int newQuantity = oldQuantity++; SqlConnection thisConnection = new SqlConnection...
1
3084
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 112: MM_editCmd.ActiveConnection = MM_editConnection Line 113: MM_editCmd.CommandText = MM_editQuery Line 114: MM_editCmd.Execute
6
2682
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 person wants to receive a newsletter and 0 no, don't want to receive it. Now.....when trying to create an UPDATE statement I am running into problems...
19
8350
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 FOR UPDATE, it is fine and no error. I also tried Set objRs = objConn.Execute("SELECT * FROM EMP UPDATE OF EMPNO"), but it still couldn't help. ...
0
2999
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 remote Windows 2003 server. Both machines are on the same domain and very close physically (<1ms ping). I have set up the Oracle linked server in...
2
11142
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 dataset to the project and drag a datatable from server explorer onto the dataset design surface. In the configuration wizard, under advanced options...
0
7465
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7398
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7656
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
5969
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
4944
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3441
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1878
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1013
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
701
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.