0

I have the following data stored in a field "call_dtls" in a mysql table. This data is stored as TEXT datatype. Data is submitted using a <textarea> control from a PHP-generated page.

Code for storing data in mysql:

$mycalltext = mysql_real_escape_string($_POST['text']);
// then storing $mycalltext in the table

The Data which gets stored in the table (each line is stored in a separate table row):

SANDY505™ (09-11-11 10:04:47): buy hinduniliver around 385-383 sl 380 trgt 390-393
SANDY505™ (09-11-11 10:05:57): buy tatasteel around 472-468 sl 464 trgt 476-480
SANDY505™ (09-11-11 10:06:09): buy nifty around 5295-5280 sl 5260 trgt 5320

When another page later fetches and displays the data, the ™ gets replaced by � and everything is displayed as a single line (the line breaks are ignored). Example:

SANDY505� (09-11-11 10:04:47): buy hinduniliver around 385-383 sl 380 trgt 390-393 SANDY505� (09-11-11 10:05:57): buy tatasteel around 472-468 sl 464 trgt 476-480 SANDY505� (09-11-11 10:06:09): buy nifty around 5295-5280 sl 5260 trgt 5320 

I want the output to be (line breaks included):

SANDY505™ (09-11-11 10:04:47): buy hinduniliver around 385-383 sl 380 trgt 390-393
SANDY505™ (09-11-11 10:05:57): buy tatasteel around 472-468 sl 464 trgt 476-480
SANDY505™ (09-11-11 10:06:09): buy nifty around 5295-5280 sl 5260 trgt 5320

Code that produces output:

$rs=mysql_query("Select * from mya_calls", $cn) or die("MySQL error: ".mysql_errno());
$number=mysql_num_rows($rs); 
while ($rsitem=mysql_fetch_object($rs)) 
    echo $rsitem->call_dtls;
outis
  • 75,655
  • 22
  • 151
  • 221
Sandy505
  • 888
  • 3
  • 15
  • 26
  • 2
    What is the character set of your database/table/connection? What is the encoding of your HTML? – Nedret Recep Nov 17 '11 at 07:24
  • Don't use [`or die`](http://www.phpfreaks.com/blog/or-die-must-die) if you're outputting HTML. You'll get invalid HTML. – outis Nov 17 '11 at 10:52
  • Outputting database error messages to non-admin users [discloses too much information](http://msdn.microsoft.com/en-us/library/ms995351.aspx#securityerrormessages_topic2). Instead, log the MySQL error message. For some errors (such as those related to missing or invalid values), output your own [error message](http://developer.apple.com/library/mac/documentation/UserExperience/Conceptual/AppleHIGuidelines/Windows/Windows.html#//apple_ref/doc/uid/20000961-TP10) to the user and what action the user can take to address it. For the rest, inform the user that there was an internal error. – outis Nov 17 '11 at 10:52
  • Are you sure the connection is using UTF-8 when both inserting and retrieving? Are you sure the column uses UTF-8? – outis Nov 17 '11 at 10:54
  • Don't use [`SELECT *`](http://stackoverflow.com/questions/321299/what-is-the-reason-not-to-use-select); select only the columns you need. – outis Nov 22 '11 at 03:40

3 Answers3

1

Doublecheck that the database connection encoding and the encoding of your outputted HTML match the encoding on your database (probably UTF-8)

DB Connection:

mysql_query('SET CHARACTER SET utf8, NAMES utf8');

Output headers:

header('Content-type: text/html;charset=utf-8');

HTML:

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
matthiasmullie
  • 2,063
  • 15
  • 17
0

got it...... :-)

the php function nl2br() does this for me. solves both issues.....

got this from googling and found solution at this site

thanks @outis for explanation about error message.

Sandy505
  • 888
  • 3
  • 15
  • 26
  • [`
    `](http://brainstormsandraves.com/articles/semantics/structure/#br) is generally non-[semantic](http://webstyleguide.com/wsg3/5-site-structure/2-semantic-markup.html) and should rarely be used. Better would be to pick the most semantically appropriate elements to structure the data, such as paragraphs or [lists](http://www.w3.org/TR/html401/struct/lists.html).
    – outis Nov 22 '11 at 03:26
0

In general, white-space in HTML is collapsed. The exceptions are within <pre> elements and elements for which the CSS white-space property is something other than "normal" (in particular, line breaks are preserved only when white-space is "pre", "pre-wrap" and "pre-line").

Displaying content on multiple lines is a presentation issue; don't use HTML to get the affect you want. Instead, pick the most appropriate elements to define the structure of the data. For example, the sample looks to be an unordered list, in which case a <ul> would be appropriate.

try {
    $result = $db->query("SELECT call_dtls FROM mya_calls");
    $result->setFetchMode(PDO::FETCH_CLASS, 'StdClass');
    ?>
    <ul>
      <?php foreach ($result as $item) { ?>
        <li><?php echo $item->call_dtls; ?></li>
      <?php } ?>
    </ul>
    <?php
} catch (PDOException $exc) {
    ...
}

Once you've done that, use CSS to set the style for the elements, if necessary.

Note that the sample code is an example of how to use semantic elements in HTML output and not necessarily suitable for production code. In development, different concerns should be separated into different modules.

outis
  • 75,655
  • 22
  • 151
  • 221
  • mixing SQL with HTML considered as a very poor design. – Your Common Sense Nov 22 '11 at 03:49
  • @Col.Shrapnel: yes, but it's better than what Sandy currently has. Plus, a diversion into an explanation of multi-tier architectures, data access and presentation layers would be too lengthy and too much to absorb at once. That's more an intermediate topic, and (not to judge) Sandy seems to be more of a beginner. Lastly, the sample code is intended as an illustration of using semantic elements rather than code intended for a production site; I'll add an explanatory disclaimer on this last point. – outis Nov 22 '11 at 10:50