wee493 Posted July 26, 2010 Share Posted July 26, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/208896-save-paragraphs-of-text-into-sql-database/ Share on other sites More sharing options...
trq Posted July 26, 2010 Share Posted July 26, 2010 but when I try to echo it out I have minimal luck. Post your code. Quote Link to comment https://forums.phpfreaks.com/topic/208896-save-paragraphs-of-text-into-sql-database/#findComment-1091174 Share on other sites More sharing options...
wee493 Posted July 26, 2010 Author Share Posted July 26, 2010 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; ?> Quote Link to comment https://forums.phpfreaks.com/topic/208896-save-paragraphs-of-text-into-sql-database/#findComment-1091175 Share on other sites More sharing options...
trq Posted July 26, 2010 Share Posted July 26, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/208896-save-paragraphs-of-text-into-sql-database/#findComment-1091189 Share on other sites More sharing options...
wee493 Posted July 26, 2010 Author Share Posted July 26, 2010 even using that code returned nothing. It's like it's getting a result but having trouble echoing it Quote Link to comment https://forums.phpfreaks.com/topic/208896-save-paragraphs-of-text-into-sql-database/#findComment-1091315 Share on other sites More sharing options...
smonkcaptain Posted July 26, 2010 Share Posted July 26, 2010 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()); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/208896-save-paragraphs-of-text-into-sql-database/#findComment-1091334 Share on other sites More sharing options...
wee493 Posted July 26, 2010 Author Share Posted July 26, 2010 I've found that my problem is <?PHP tags. When "<?PHP" or "?>" is saved in a database an d tried to echo it messes things up. How can I input and output PHP tags without it trying to run the code? Quote Link to comment https://forums.phpfreaks.com/topic/208896-save-paragraphs-of-text-into-sql-database/#findComment-1091437 Share on other sites More sharing options...
DavidAM Posted July 26, 2010 Share Posted July 26, 2010 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); Quote Link to comment https://forums.phpfreaks.com/topic/208896-save-paragraphs-of-text-into-sql-database/#findComment-1091451 Share on other sites More sharing options...
wee493 Posted July 26, 2010 Author Share Posted July 26, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/208896-save-paragraphs-of-text-into-sql-database/#findComment-1091485 Share on other sites More sharing options...
trq Posted July 26, 2010 Share Posted July 26, 2010 What does the outputted html source look like? eg; Right click-> View Source. Quote Link to comment https://forums.phpfreaks.com/topic/208896-save-paragraphs-of-text-into-sql-database/#findComment-1091486 Share on other sites More sharing options...
wee493 Posted July 26, 2010 Author Share Posted July 26, 2010 I've managed to get it working for the most part by doing $text = str_replace("\n", '<br>', $get); print($text); Quote Link to comment https://forums.phpfreaks.com/topic/208896-save-paragraphs-of-text-into-sql-database/#findComment-1091494 Share on other sites More sharing options...
jcbones Posted July 26, 2010 Share Posted July 26, 2010 Or, you could use native functions echo nl2br($text); nl2br() Quote Link to comment https://forums.phpfreaks.com/topic/208896-save-paragraphs-of-text-into-sql-database/#findComment-1091497 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.