??British Expat ??Vancouver ❤ Gardening ?Founder of gitSQL

Category: PHP

Stripslashes in SQL for MYSQL and ORACLE

Problem

Today, I wanted to strip slashes from a comment field using SQL (INSTEAD of using php stripslashes).

Why you ask?

I was working on an application which had not considered stripslashes for formatting output.

So I had a look and realised. Do I change 40,000 lines of code to add Stripslashes, or 1 line at the SQL select statement?

OK, slight exaggeration, but you get what I mean.

Solution

MySQL

[sourcecode language=’sql’]
Select
Replace(field_name, ‘\’, ”) as stripped_field
from
table;
[/sourcecode]

Oracle

[sourcecode language=’sql’]
Select
Replace(field_name, ”, ”) as stripped_field
from
table;
[/sourcecode]

PHP EXTRA

If using in PHP add an extra backslash like this;

[sourcecode language=’sql’]
// strip slashes as part of the SQL select statement
$sql = “Select Replace(field_name, ‘\’, ”) as stripped_field from table”;
[/sourcecode]

Please get in touch if you know of an easier way to do this, for example, Oracle Functions, or MySQL functions.

Many thanks and happy coding.

PRE tags, the quick way to keep formatting

I get asked quite often if there is a way to show CODE on a website, with it’s formatting in place?

The short answer is <pre>

It can output free text (i.e. non server side code) in a pre-formatted way. Yep, PRE = Pre-formatted.

On top of that, Preformatted also renders the php print_r() output in a nicely nested unordered list, which is great for debug purposes;

e.g.
[sourcecode language=’php’]  echo ”

";    print_r($arr);  echo "

“; [/sourcecode]

I am sure there are other uses for the <pre> tag too, I have just scratched the surface.

Give it a go – let me know if you find other uses for it.

Thanks.

Internet Explorer 7 – Accessibility Options

These settings are a web developers worst nightmare…. what happens if a web browser ignores the website FONTS? or worst still the STYLESHEETS?

Well – i guess if the website is built with accessibilty in mind, then there is no problem;

The page will render correct, no matter what browser or platform… but… if you have to use absolute positioning, and specific font sizes… then please please bear these settings in mind.

IE7 Accessibility Options

Tools | Internet Options | Accessibility.

Happy coding people!

Split a Price (FLOAT) into Pounds (£) and Pence (p)

Whilst working on an E-commerce website, written in PHP i came across and issue where i had a price stored in a table as a float.

I wanted to split the pounds, and pence into two variables to make it easier to work with, but then save the results back into 1 field. So this is what i did.

http://uk2.php.net/manual/en/function.explode.php#63110

[sourcecode language=’php’]
//If you want to split a price (float) into pounds and pence.
//or dollors and cents etc etc.

$price = “6.20”;

$split = explode(“.”, $price);
$pound = $split[0]; // piece1
$pence = $split[1]; // piece2

echo “&pound $pound . $pencen”;

[/sourcecode]

Enjoy 🙂

Website Copyright Date

Have you noticed that some websites have a copyright date which is wrong?

For example:

www.somecompany.com (I appologise if you own this site)

then on the site, you have, Copyright SomeCompany Ltd 2002-2006

Now, if your lucky, your site is designed so that the footer can be changed once, so the entire site is updated. Well done.
For the others… shame on you. – Redesign your site.

Anyway.. heres a way to crack it on the head once and for all.
[sourcecode language=’php’]
//PHP
2003 – < ?php echo date("Y"); ?>
[/sourcecode]
[sourcecode language=’php’]
//ASP
2003 – < %= year %>
[/sourcecode]
This way, as long as your server date is correct, your website will always be up to date. In fact, consider using date more in this way, to save yourself manual intervention.

Powered by WordPress & Theme by Anders Norén