PHP & Sqlite Question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
SpecialK
Forum Commoner
Posts: 96
Joined: Mon Sep 18, 2006 3:49 pm

PHP & Sqlite Question

Post by SpecialK »

I am using an Sqlite database and can't find anything how to use escaped quotes when inserting.

Prior to the query, I call addslashes on the variables

Code: Select all

 
$article = addslashes($article);
$query = "INSERT INTO news (date,title,article) VALUES ('$date','$title','$article')";
 
Apparently sqlite doesn't like this way of escaping characters, so what would be the best solution? I need to be able to insert single and double quotes, so am a bit stumped when in comes to the sql and php combination.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: PHP & Sqlite Question

Post by Christopher »

Each database has its own escaping function. You need to always use the database specifc one.

http://us.php.net/manual/en/function.sq ... string.php
(#10850)
User avatar
SpecialK
Forum Commoner
Posts: 96
Joined: Mon Sep 18, 2006 3:49 pm

Re: PHP & Sqlite Question

Post by SpecialK »

Thanks!

I never knew about this command previously, definately a huge help for the future
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Re: PHP & Sqlite Question

Post by Stryks »

What version of sqlite are you trying to use?

If you use 3 (apparently faster but not without issues) you need to use PDO, and can use the prepare() statement to automatically escape values and lock down queries.
User avatar
SpecialK
Forum Commoner
Posts: 96
Joined: Mon Sep 18, 2006 3:49 pm

Re: PHP & Sqlite Question

Post by SpecialK »

This is currently version 2, but the new server to migrate to in the coming while will be version 3.
I'm not looking forward to that as I don't really like the differences of PDO, but I may come to like it more with actual use.
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: PHP & Sqlite Question

Post by alex.barylski »

SpecialK wrote:This is currently version 2, but the new server to migrate to in the coming while will be version 3.
I'm not looking forward to that as I don't really like the differences of PDO, but I may come to like it more with actual use.
PDO === PITA :P
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Re: PHP & Sqlite Question

Post by Stryks »

Hockey wrote:PDO === PITA :P
Totally with you on that. I never DID find a way to return affected rows.

Definite PITA, but I did run a few comparison tests when I was deciding which version to use and in my 'real-world' testing, V3 was the definite winner when it came to speed. As I mentioned previously, this speed difference is apparently the norm.

In my case, the speed was more important than ... well .. ease of use or full functionality. :lol:
User avatar
liljester
Forum Contributor
Posts: 400
Joined: Tue May 20, 2003 4:49 pm

Re: PHP & Sqlite Question

Post by liljester »

Stryks wrote:
Hockey wrote:PDO === PITA :P
Totally with you on that. I never DID find a way to return affected rows.

Code: Select all

$update = $db->prepare("UPDATE tabe SET column = 'value';");
$update->execute();
print $update->rowCount();
i believe that will do it for you :)
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Re: PHP & Sqlite Question

Post by Stryks »

liljester wrote: i believe that will do it for you :)
You'd think so, but it doesn't. It returns 0. It always returns 0, even when I have results that prove otherwise.

I'll be picking that project up again soon, and I'm thinking I might update my php install. Maybe I just had a version with issues. (if anyone is going to, it'll be me :roll: )
Post Reply