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

String variable read from Mysql DB + echo = Newline problem

Hi. I have this simple code:

===========================================
->Database query here

(.. some code)

$row=mysql_fetch_array($res);

(...)

$formatting2 = $row['formatting2"];

(..)

//Echo variable from DB
echo "$formatting2";

$formatting2 = "USERNAME: %1\$s \r\nSERIAL: %2\$s \r\n\r\n";
//Echo same string - this time from an "inline" variable
echo "$formatting2";
===========================================

Ok, maybe that was overly complicated. I hope you follow anyway. Whats
happening here is that I am getting a formatted string from a database
which I throw in a variable. What you see here is my test case to
visualize the problem. Now, when I echo this variable or pass it to
printf/sprintf then I get a different result than if I "manually"
create the variable inline in my php script.

So, the first echo $formatting2 there will output:

USERNAME: %1$s \r\nSERIAL: %2$s \r\n\r\n

whilst the second shows the newlines correctly. Also printf/sprintf
barfs if I pass the string I got from the DB.

What the heck is going on here? Database returning some strange string
format?
Thanks :)

Arni Johannesson

Mar 20 '06 #1
4 3400
weirdstuff wrote:
Hi. I have this simple code:

===========================================
->Database query here

(.. some code)

$row=mysql_fetch_array($res);

(...)

$formatting2 = $row['formatting2"];

(..)

//Echo variable from DB
echo "$formatting2";

$formatting2 = "USERNAME: %1\$s \r\nSERIAL: %2\$s \r\n\r\n";
//Echo same string - this time from an "inline" variable
echo "$formatting2";
===========================================

Ok, maybe that was overly complicated. I hope you follow anyway. Whats
happening here is that I am getting a formatted string from a database
which I throw in a variable. What you see here is my test case to
visualize the problem. Now, when I echo this variable or pass it to
printf/sprintf then I get a different result than if I "manually"
create the variable inline in my php script.

So, the first echo $formatting2 there will output:

USERNAME: %1$s \r\nSERIAL: %2$s \r\n\r\n

whilst the second shows the newlines correctly. Also printf/sprintf
barfs if I pass the string I got from the DB.

What the heck is going on here? Database returning some strange string
format?
Thanks :)

Arni Johannesson


Arni,

No, this is operating as expected.

In PHP strings, "\n" is a newline character. However, when read in from
an external source, these are the characters "backslash" and "en". The
equivalent in PHP would be "\\n" - the backslash is escaped and the
letter "n" follows.

But this is how it should be. External data should *never* be language
dependent - what happens if, for instance, you wanted the characters
blackslash and en in some text? For PHP you'd use "\\n", but for COBOL
the data would have to be "\n".

So, what you have to do is change your formatting characters. Replace
"\\n" with "\n" and "\\r" with "\r", for instance.

It should resolve your problem.

Or, alternatively, put the actual characters themselves in the database.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Mar 20 '06 #2
Errr, what? :)

Ok I had figured that a backslash was being added when I read the
string from DB, but all my attempts of correcting that have failed.
What we have here is a case where knowing how the output is formatted
_exactly_ is very important. So it would be nice to be able to
"manually" format stuff like newlines and breaks.

Now, your suggestions:
So, what you have to do is change your formatting characters. Replace
"\\n" with "\n" and "\\r" with "\r", for instance.
The string already has single backslash in the database. If I add
another "just for fun" I just get two in the output. If I then do a
stripslashes() on one or the other then that doesn't help either.
Or, alternatively, put the actual characters themselves in the database.


Thats exactly what I'm trying not to do.
So we can agree that the problem is when I am getting the string from
the database. Then PHP (or MySQL?) does some funkyness to the string
variable which makes PHP think that \n means it should print
"backslash" and "en"

Jolly good show. Now lets fix it! :)

Mar 20 '06 #3
weirdstuff wrote:
Errr, what? :)

Ok I had figured that a backslash was being added when I read the
string from DB, but all my attempts of correcting that have failed.
What we have here is a case where knowing how the output is formatted
_exactly_ is very important. So it would be nice to be able to
"manually" format stuff like newlines and breaks.

Now, your suggestions:

So, what you have to do is change your formatting characters. Replace
"\\n" with "\n" and "\\r" with "\r", for instance.

The string already has single backslash in the database. If I add
another "just for fun" I just get two in the output. If I then do a
stripslashes() on one or the other then that doesn't help either.

Or, alternatively, put the actual characters themselves in the database.

Thats exactly what I'm trying not to do.
So we can agree that the problem is when I am getting the string from
the database. Then PHP (or MySQL?) does some funkyness to the string
variable which makes PHP think that \n means it should print
"backslash" and "en"

Jolly good show. Now lets fix it! :)


Please read it again. Replacing "\\n" with "\n" will not add another
backslash. It will convert the backslash and en to a newline character.

You really only have two choices. Either put the characters themselves
in the database, or put something representing the characters in the
database and substitute after you read them out.

And BTW - you cannot exactly control formatting on the user's browser.
All you can do is recommend formatting. If you want exact, create a PDF.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Mar 20 '06 #4

Alrighty cool. Thanks Jerry. I can be a bit slow sometimes ;)

I'll follow your suggestions. I think the replace option will be the
way to go.

PS: The output is read by remote automated systems which I have no
control over. Thus the stringent formatting rules.

Again, thanks...!

Mar 20 '06 #5

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

Similar topics

1
by: Larryd | last post by:
I am new at php/mysql. I am having trouble with looping. What Im trying to do is have multiple part numbers under 1 variable. If i just put $pnum = thepartnumber and echo $fprice, it will take the...
10
by: Jack | last post by:
How would I add a variable that I will assign to a list of $_POST variables that I extract from a form? My form passes a value for $q. That works fine. What I want to do is run an if/else on it...
1
by: Micke | last post by:
I have a problem with string insert that I hope someone can help me with. I'm doint a one-line insert into my database from an other program and it works perfect (thanks to Bill Karwin): ...
4
by: Jim Michaels | last post by:
<?php include 'dbinc.php'; $result = mysql_query("SELECT image,mime_type FROM photos WHERE image_id=".intval($_GET), $link) or die("MyErr:".mysql_error()); if ($row = mysql_fetch_assoc($result))...
8
by: Jerry | last post by:
I am a MySQL and PHP newbie. I am having trouble getting the $w variable in my code below passed to mysql. When I use the value of $w directly in the Where clause, the correct rows are returned....
2
by: David Dawson | last post by:
I have forgotten a lot about SQL and would like to be (gently) reminded how to do this: In a MySQL query on the database (one table with 15 variable length fields, I want to put each field into a...
8
by: Peter Gustafsson | last post by:
Hi I have a HTML-only file, and asp file that do a databasequery. How can I do a query in the html file, like this: DBLookup("2"). The asp file execute the query and respond with the result....
5
by: strawberry | last post by:
In the function below, I'd like to extend the scope of the $table variable such that, once assigned it would become available to other parts of the function. I thought 'global $table;' would solve...
3
by: Siong.Ong | last post by:
Dear all, my PHP aims to update a MySQL database by selecting record one by one and modify then save. Here are my PHP, but I found that it doesnt work as it supposed to be, for example, when...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.