PHP script crashes mysteriously using nl2p function

Hi everyone, thanks for taking the time to read this.

I am using the following PHP script to retrieve text from a mySQL database, format it using the nl2p function and display it on my site. The code works fine if I use it once but if I use the code twice on different parts of the same page the site crashes (that’s to say nothing whatsoever appears on the screen except whiteness).

If I take the nl2p function out of the code I can use the code over and over again on the same page.

Any ideas what’s wrong?

Leao


<?php
$con = mysql_connect("mysql.com","username","password");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("mydatabase", $con);

$result = mysql_query("SELECT * FROM mytable");

while($row = mysql_fetch_array($result))
  {
  $collegeshort = $row['collegeshort'];
  }

mysql_close($con);

function nl2p($formattingfunction)
{
$formattingfunction = str_replace("\\r\
\\r\
", "</p><p>", $formattingfunction);
$formattingfunction = str_replace("\\r\
", "<br>", $formattingfunction);
$formattingfunction = str_replace("...", "&hellip;", $formattingfunction);
return $formattingfunction;
}

echo nl2p($collegeshort); 
?>

Great, I’m glad you got it sorted. :slight_smile:

Thanks.

I understand now that I can just declare the nl2p function once and just use it (without declaring it again) afterwards. I assumed that every time I closed a PHP statement ?> I would have to redeclare everything once again.

Leo

I would guess you’re redefining the function, which isn’t allowed.

Replace…


  function nl2p($formattingfunction){
    $formattingfunction = str_replace("\\r\
\\r\
", "</p><p>", $formattingfunction);
    $formattingfunction = str_replace("\\r\
", "<br>", $formattingfunction);
    $formattingfunction = str_replace("...", "&hellip;", $formattingfunction);
    return $formattingfunction;
  }

…with…


if(false === function_exists('nl2p')){
  function nl2p($formattingfunction){
    $formattingfunction = str_replace("\\r\
\\r\
", "</p><p>", $formattingfunction);
    $formattingfunction = str_replace("\\r\
", "<br>", $formattingfunction);
    $formattingfunction = str_replace("...", "&hellip;", $formattingfunction);
    return $formattingfunction;
  }
}