473,698 Members | 2,588 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

String variable read from Mysql DB + echo = Newline problem

Hi. I have this simple code:

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

(.. some code)

$row=mysql_fetc h_array($res);

(...)

$formatting2 = $row['formatting2"];

(..)

//Echo variable from DB
echo "$formattin g2";

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

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 3426
weirdstuff wrote:
Hi. I have this simple code:

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

(.. some code)

$row=mysql_fetc h_array($res);

(...)

$formatting2 = $row['formatting2"];

(..)

//Echo variable from DB
echo "$formattin g2";

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

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*******@attgl obal.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*******@attgl obal.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
2014
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 1st value and carry it down till the end. Below is my current code. :code begin: <? $connection = mysql_connect("localhost","user","pass") or die("Unable to connect to localhost");
10
2666
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 and assign a new variable based on what was chosen such as if ($q == "red") { $q = "tp"; //change $q to this value $sub = "ak"; //attach this value to the $sub variable } else {
1
1603
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): postupdate mysql -u -p vader -e "INSERT INTO station (datum,tid,temp1,wspeed,wdeg,wdrpoint,wchill,press,hum,temp2,temp3,heatindex,tdew1) VALUES (NOW(),NOW(),$t1$,$wspkph$,$wdrdeg$,$wdrpoint$,$wchillc$,$barmbar1$,$rh1$,$trhc1$,$tb1$,$heatindexc$,$dpc1$)"
4
1591
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)) { $ext=array('image/jpeg'=>'jpg','image/gif'=>'gif','image/png'=>'png','image/x-png'=>'png'); header("Expires: Mon, 26 Jul 2030 05:00:00 GMT"); header("Content-Type: $row"); header("Content-Disposition: inline; $_GET.".$ext]);
8
2842
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. However, when I try to use the variable in the Where clause, either an error occurs or no rows are returned. Any thoughts greatly appreciated! I am using php-4.4.2 and MySQL-4.1.18-0 on Suse 9.1 Jerry
2
6804
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 Bash variable so that I can handle each field as an entity. The query I have is something like this: mysql -e "use $database; select field1, field2, field3..., from Table1 where fieldN like '%something%';"
8
2271
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. Send it back to the htmlfile and put it were the "DBLookup("2")" is. I can not have any asp code in the html page.
5
2270
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 this but it's clear that I'm misunderstanding $variable persistence. I posted a similar enquiry over at alt.php.mysql, but I guess this is a more appropriate forum because the problems I'm having relate to PHP. Any help appreciated. ...
3
2145
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 Record (i) is shown and modified, the change will come to Record (i+1). Can anyone provide suggestion? thanks.
0
8680
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9169
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9030
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8871
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7738
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6528
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5861
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3052
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2335
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.