Jump to content

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.