homepage Welcome to WebmasterWorld Guest from 54.167.11.16
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Visit PubCon.com
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
brancook




msg:3502433
 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=$_POST['searchterm'];
$searchterm= trim($searchterm);

if (!$searchterm) {
echo 'You have not entered a search term. Please try again.';
exit;
}
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>';
}
?>

 

russkern




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

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

[webmasterworld.com...]

R

brancook




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

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

PHP_Chimp




msg:3503024
 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);

brancook




msg:3503233
 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