Solved

MYSQL PHP: How do we remove or escape ; (semi-colon) in data retrieved from a MySQL table field?

Posted on 2008-06-15
6
4,489 Views
Last Modified: 2013-12-13
PHP MYSQL

Hello,

In a MySQL table field, I have ; (semi-colon) in it. When I retrieve the data in a PHP script, I get premature termination of the script because of the ; (semi-colon)


Example of table field:
"The quick brown fox jumps over the lazy dog; so does the elephant"
(semi-colon after dog)


Example of PHP script:

...
$result = mysql_query($query);
while ($row = mysql_fetch_assoc($result)) {
extract($row);
echo "$contents";
}


What do I do?
0
Comment
Question by:gingera
6 Comments
 
LVL 4

Expert Comment

by:afzz
ID: 21788003
why do you extract? it is unsafe

why dont you try this
$result = mysql_query($query);
while ($row = mysql_fetch_assoc($result)) {
//extract($row);
echo "$row->contents";
}
0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 450 total points
ID: 21788454
Gingera: the extract() command is not likely to be the problem.  See here:
http://us2.php.net/manual/en/function.extract.php

The data may not be getting stored in the data base in the first place!  It could be an escaping error.

Do you have phpMyAdmin?  If so, look at the data tables to see what is there.

Look at mysql_real_escape_string().  It will help keep the contents clean.
http://us2.php.net/manual/en/function.mysql-real-escape-string.php

You might also want to purify your character set on the in-bound side of the data base.  See code snippet.  HTH, ~Ray
function get_clean_text_string($string) 

{ // FORCE IT TO DB-ACCEPTABLE ESCAPABLE CHARACTERS ONLY

	$new	= trim(ereg_replace('[^\' a-zA-Z0-9&!#$%()"+:?/@,_\.\-]', '', $string));

	$new	= ereg_replace(' +', ' ', $new);

return ( $new );

}

Open in new window

0
 
LVL 2

Assisted Solution

by:ayus80
ayus80 earned 50 total points
ID: 21800363
replace your code with this
$result = mysql_query($query);

while ($row = mysql_fetch_assoc($result)) {

echo ereg_replace(';', '', $row['contents']);

}

Open in new window

0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 450 total points
ID: 21815902
Gingera: Any news to report?

@ayus80: ereg is a little slow in PHP.  For a single character better performance is available from str_replace().

Best, ~Ray
$result = mysql_query($query);

while ($row = mysql_fetch_assoc($result)) {

    echo str_replace(';', '', $row['contents']);

}

Open in new window

0
 

Author Comment

by:gingera
ID: 21840417
Hi Ray, sorry for the delay. I've just managed to test out the suggestions.

Your help is very much appreciated!
0
 

Author Closing Comment

by:gingera
ID: 31467315
THANKS!!!
0

Featured Post

Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
TCPDF Table of Contents - Page Numbers Don't Right Align 2 34
Is this error or Notice in php error log? 6 31
Site hacked - decoding the PHP? 15 55
Session timeout 5 13
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

948 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now