473,386 Members | 1,602 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,386 software developers and data experts.

Proper Way To Store, Retrieve Text To/From PostgreSQL

I want to post text field data from these HTML TEXTAREA tags to a
PostgreSQL database and have it reappear back on another page exactly
as I had typed it. Over the years I have done this but only did it
with simple text. This time around, I want to handle much more complex
text. I need to preserve some kinds of features.

(Yes, I have turned off magic quotes.)

- Need to strip diacritics. I learned I could use htmlentities to
catch &?grave;, &?acute;, &?circ; &?uml; &?tilde; &?cedil; and the ?
can become the character I wish to preserve and strip the others.

- Need all text to be saved in US-ASCII format. Anything outside of
that can simply be ? character.

- Need to save all \r and \n as literally that in the database so that
SQL selects show everything on one line. This makes it easier to deal
with the records with the psql command.

- Need to preserve all US-ASCII characters so that things, such as ',
", \, <, >, %, &, and --, are preserved. I want the database to be
able to store these items and not munge them on me reading them back
out. For instance, if someone types in HTML, PHP source, or SQL
language code into a text field, I want the database to store it and
let me retrieve it without munging it.

- I fear storing the data as htmlentity, hexadecimal, or octal because
it adds huge width to the text column in the database. So, storing it
in some other format like slash-delimited would be best.

Basically I have a $web object with a Request('fieldname') method and
I need this function to handle the above for me on reading field data
so that it's ready to post to the database. I then have another
$strings object with a PrepareTextArea($s) method that helps me
translate this database data back into text prepared for either
ordinary HTML display or to put back into the TEXTAREA gadget on the
page.

Any advice would be greatly appreciated.

Feb 13 '07 #1
3 4990
Alfred wrote:
- I fear storing the data as htmlentity, hexadecimal, or octal because
it adds huge width to the text column in the database. So, storing it
in some other format like slash-delimited would be best.
Encode:

$encoded = addcslashes($raw, "\0..\37!@\177..\377");

(Remember that you must still use pg_escape_string() before you insert into
the database!)

Decode:

$raw = stripcslashes($encoded);

--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact
Geek of ~ HTML/SQL/Perl/PHP/Python*/Apache/Linux

* = I'm getting there!
Feb 13 '07 #2
Alfred wrote:
I want to post text field data from these HTML TEXTAREA tags to a
PostgreSQL database and have it reappear back on another page exactly
as I had typed it. Over the years I have done this but only did it
with simple text. This time around, I want to handle much more complex
text. I need to preserve some kinds of features.

(Yes, I have turned off magic quotes.)

- Need to strip diacritics. I learned I could use htmlentities to
catch &?grave;, &?acute;, &?circ; &?uml; &?tilde; &?cedil; and the ?
can become the character I wish to preserve and strip the others.

- Need all text to be saved in US-ASCII format. Anything outside of
that can simply be ? character.

- Need to save all \r and \n as literally that in the database so that
SQL selects show everything on one line. This makes it easier to deal
with the records with the psql command.

- Need to preserve all US-ASCII characters so that things, such as ',
", \, <, >, %, &, and --, are preserved. I want the database to be
able to store these items and not munge them on me reading them back
out. For instance, if someone types in HTML, PHP source, or SQL
language code into a text field, I want the database to store it and
let me retrieve it without munging it.

- I fear storing the data as htmlentity, hexadecimal, or octal because
it adds huge width to the text column in the database. So, storing it
in some other format like slash-delimited would be best.

Basically I have a $web object with a Request('fieldname') method and
I need this function to handle the above for me on reading field data
so that it's ready to post to the database. I then have another
$strings object with a PrepareTextArea($s) method that helps me
translate this database data back into text prepared for either
ordinary HTML display or to put back into the TEXTAREA gadget on the
page.

Any advice would be greatly appreciated.
First of all, htmlentities() is for displaying data in html - not
storing in the database.

Then use pg_escape_string to encode your data and store it in your
database. Retrieve it then use htmlentities() to display the data.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Feb 13 '07 #3
On Feb 13, 6:40 pm, Jerry Stuckle <jstuck...@attglobal.netwrote:
First of all, htmlentities() is for displaying data in html - not
storing in the database.

Then use pg_escape_string to encode your data and store it in your
database. Retrieve it then use htmlentities() to display the data.
Thanks, didn't know about that one. I'll give it a try.

Feb 14 '07 #4

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

Similar topics

6
by: James Turner | last post by:
I am trying to store formatted text (windows format) into a MySQL database and then retrieve it. The field in the database is a varchar. I cut and paste the test into a form field formatted, then...
5
by: Roy Gourgi | last post by:
Hi, I am used to working in Visual FoxPro and I would like to be able to create a database and store and retrieve information from it. What is the simplest way to do it and what should I be...
5
by: Guadala Harry | last post by:
What are my options for *securely* storing/retrieving the ID and password used by an ASP.NET application for accessing a SQL Server (using SQL Server authentication)? Please note that this ID and...
3
by: Solution Seeker | last post by:
I want to Store the String value with Single Quotes in the Field of Database where if i try to Store the String value with Single Quotes (as it is) then it is throwing the error as SQL String...
0
by: Jean-Michel POURE | last post by:
Dear friends, I am currently testing Ulogd ip traffic logging system with PostgreSQL. It works in conjunction with GNU/Linux iptables. The Ulogd project can be found here:...
2
by: Don Kelloway | last post by:
I'm a first-time user with PostgreSQL so please forgive my ignorance. I've purchased (and read) Practical PostgreSQL (O'Reilly) and PostgreSQL Essential Reference (New Riders). So far, so good. ...
4
by: alexandre.brisebois | last post by:
Hi, I am using access 2003, I would like to know if there is an option to reorganize the tables in a maner that is readable, as we can do in sql sever 2000 or 2005. I have been given a database...
3
by: JM | last post by:
Before storing information from a form in database I perform follwing operations on it : $path = mysql_real_escape_string(strip_tags(trim(urldecode($_POST)))); $summary =...
3
by: =?Utf-8?B?RGV2b24=?= | last post by:
Is it possible to retrieve a proper/invariant file or directory name using a lowercase full path string without having to resort to a call to GetDirectories()/GetFiles() using the...
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:
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.