Question

Ok, so I have a database and am populating a table. I connect and populate with no problem with $sqlCommand via a php page.

my problem is one of my fields has blocks/paragraphs of content. I need paragraphs to show, but nl2br() isn't work.

Here's how I populate (site.com/xtblcreate.php):

// Create table1 in db x for storing words
 $sqlCommand = "CREATE TABLE table1 (
           id INT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY,
           a TEXT,
           b TEXT,
           c TEXT,
           d TEXT,
   page_views INT NOT NULL default '0',
           FULLTEXT (a,b,c,d)
           ) ENGINE=MyISAM";
  $query = mysql_query($sqlCommand) or die(mysql_error());
  echo "<h3>Success creating table1 in db x</h3>";

   // Insert dummy data into the table1 in db x
   $sqlCommand = "INSERT INTO table1 (a,b,c,d) VALUES
          ('something', 'something else', 'a lot \n of info', 'quick conclusion' )"; 

What I need is " a lot of info" to be broken as like two paragraphs.

Then the query on its own php page ( site.com/x.php):

 $search_output .= "$count result(s) for <strong>$searchquery</strong><br />";
 while($row = mysql_fetch_array($query)){
        $id = $row["id"];
        $a = $row["a"];
        $b = $row["b"];
        $c = $row["c"];
        $d = $row["d"];

    $search_output .= "*<br>$a- <br/><b>c: </b>$c<br /> <br /> b: $b<br />finally, d:      $d<br/>";  
echo nl2br($c);
  //output $c with paragraphs

please go easy on me as I'm new at this.

Was it helpful?

Solution

Escape sequences such as \n aren't processed when using single quotes in PHP.

If you changed your query to:

$sqlCommand = "INSERT INTO table1 (a,b,c,d) VALUES
      ('something', 'something else', \"a lot \n of info\", 'quick conclusion' )"; 

Then I suspect nl2br() will yield your desired output.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top