homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

cut down description from database

 1:24 am on Nov 12, 2007 (gmt 0)

I'm displaying a description from a database using php. The descriptions that are in the database are going to be fairly large. I don't want to display the entire description on the webpage, only a portion of it. Here is my code:

$dbh=mysql_connect ("localhost", "username, "password") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("search");
$searchterm= trim($searchterm);

if (!$searchterm) {
echo 'You have not entered a search term. Please try again.';
if (!get_magic_quotes_gpc()) {
$searchterm = addslashes($searchterm);

$query = "SELECT * FROM site_data WHERE description LIKE '%$searchterm%'";
$result = mysql_query($query);

$num_results = mysql_num_rows($result);

echo "<p>Number of Results found for <i><strong>$searchterm</strong></i>: $num_results</p>";

for ($i=0; $i <$num_results; $i++) {
/*$row = $result->fetch_assoc();*/
$row = mysql_fetch_assoc($result);
echo '<p>'.($i+1).': ';
echo stripslashes($row['page_title']);
echo '<br />';
echo stripslashes($row['description']);
echo '<br />';
echo stripslashes($row['url']);
echo '</p>';



 2:09 am on Nov 12, 2007 (gmt 0)

I had the same question a while back... check this thread.




 2:11 am on Nov 12, 2007 (gmt 0)

Thanks I'll try out the suggestions from that thread.


 7:24 pm on Nov 12, 2007 (gmt 0)

Although this is off topic -
if (!get_magic_quotes_gpc()) {
$searchterm = addslashes($searchterm);

You are only adding slashes to your data, not using the mysql_real_escape_string [uk2.php.net], so your string could be safer.
You may want to use something like -

if (get_magic_quotes_gpc()) {
$searchterm = stripslashes($searchterm);
$searchterm = mysql_real_escape_string($searchterm);


 11:18 pm on Nov 12, 2007 (gmt 0)

Thanks for the tip.

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved