Jump to content

Save Paragraphs of Text into SQL database


wee493

Recommended Posts

I'm wanting to save snipets into an sql database and be able to display them later (Similar to pastebin and those types of sites). I've already setup a table with the type set to text and have syntax highlighting taken care of. On the way in I'm doing mysql_real_escape_string() on the text but when I try to echo it out I have minimal luck. Can anyone offer any suggestions/help?

Trying the following displays nothing

include('includes/session.php');

$result = mysql_result(mysql_query("SELECT text FROM text WHERE id = '1'"), 0);

echo $result;

 

 

This is what is saved in that row on the DB

<?PHP

$name = \'Wee493\';
$string = \'Hello World this is \'.$name.\'!\';

echo $string; 

?>

That is a terrible method of querying a database. Absolutely no error handling.

 

Try:

 

<?php
include('includes/session.php');
if ($result = mysql_query("SELECT text FROM text WHERE id = '1'")) {
  if (mysql_num_rows($result)) {
    echo mysql_result($result, 0);
  } else {
    echo "No records found";
  }
} else {
  trigger_error(mysql_error());
}

 

ps: You don't actually expect the stored php to execute do you?

Or you could do something like this:

 

<?php
include('includes/session.php');
$result = mysql_query("SELECT text FROM text WHERE id = '1'"));
$get=mysql_fetch_row($result);
$rows=mysql_num_rows($result);

if($rows < 1){
    echo "No records found";
}else {
echo $get['text'];
} else {
  trigger_error(mysql_error());
}

?>

If you are echoing to a webpage, the browser is seeing '<' as the start of an HTML tag. It does not know what '?php' is (it is not a known tag) so it does not display it.  This is standard (required) browser behavior so that new HTML tags do not break pages in older browsers (they just get ignored). 

 

To display this text, use the htmlentities() function.  It will convert any, well, HTML entities, into special codes so the browser will display them instead of interpreting them:

echo htmlentities($string);

If you are echoing to a webpage, the browser is seeing '<' as the start of an HTML tag. It does not know what '?php' is (it is not a known tag) so it does not display it.  This is standard (required) browser behavior so that new HTML tags do not break pages in older browsers (they just get ignored). 

 

To display this text, use the htmlentities() function.  It will convert any, well, HTML entities, into special codes so the browser will display them instead of interpreting them:

echo htmlentities($string);

That did not help.

 

I just cant echo php, plain text is fine. I've even tried base 64 encoding/decoding with no luck.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.