Board index   FAQ   Search  
Register  Login
Board index PHP PHP General

Site Search Problems: Need Help!

General discussions related to php

Moderators: macek, egami, gesf

Site Search Problems: Need Help!

Postby stevebob » Sat Jul 19, 2003 9:48 am

Hi! I am currently attempting to add a site search to my website, and a strange problem is occuring. I am storing key words formatted out of each page via a formatting script I found at php.net into an sql database. I am trying to create an automatic "spidering" system. Here's the code:
$username="username";
$password="pass";
$database="dbname";
//table name: search
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

$query= "SELECT * FROM search";
$result=mysql_query($query);
$num=mysql_numrows($result);

$i=0;
while ($i < $num)
{
$page=mysql_result($result,$i,"page");
//`page` contains file name
$document = file_get_contents($page);
//PHP.net code:
$search = array ("'<script[^>]*?>.*?</script>'si", //
Strip out javascript
"'<A[^>]*?>.*?</A>'si", // Strip out links
"'<[\/\!]*?[^<>]*?>'si", // Strip out html tags
"'([\r\n])[\s]+'", // Strip out white space
"'&(quot|#34);'i", // Replace html entities
"'&(amp|#38);'i",
"'&(lt|#60);'i",
"'&(gt|#62);'i",
"'&(nbsp|#160);'i",
"'&(iexcl|#161);'i",
"'&(cent|#162);'i",
"'&(pound|#163);'i",
"'&(copy|#169);'i",
"'&#(\d+);'e"); // evaluate as php

$replace = array ("",
"",
"",
"\\1",
"\"",
"&",
"<",
">",
" ",
chr(161),
chr(162),
chr(163),
chr(169),
"chr(\\1)");
//end PHP.net code
$text = preg_replace ($search, $replace, $document);

$id=mysql_result($result,$i,"id");
//`words`: where formatted text goes
$query2 = "UPDATE `search` WHERE `id` = '$id' SET `words` = '$text'";
mysql_query($query2);

++$i;
}

mysql_close();


Now, that should work. Load each file, format the HTML and JavaScript out, and put the remaining text in a Database, right? Wrong! For some reason, the first 2 entries in the table contain nothing in the `word` column. What could possibly be causing that?
stevebob
New php-forum User
New php-forum User
 
Posts: 2
Joined: Sun Jun 22, 2003 12:46 pm
Location: Florida

Postby swirlee » Sat Jul 19, 2003 10:09 am

stevebob, the first step in solving a MySQL problem is seeing if your query actually works. So instead of running your query straight away, echo it and see what it looks like. If it still looks alright, copy and paste it to the MySQL console (mysql.exe, try "mysql --help" for more info) and see if it works. If it works, then you know that it's a PHP problem, not a MySQL problem.

My guess is that there's something wrong with your regular expressions, but that's not my department. Give the above a go and let us know what happens.

And as a side note, you don't need to use backticks (`) to surround column names unless your column names happen to be reserved words or have unusual markup, neither of which seems to be the case.
User avatar
swirlee
Moderator
Moderator
 
Posts: 2272
Joined: Sat Jul 05, 2003 1:18 pm
Location: A bunk in the back

Re: Site Search Problems: Need Help!

Postby WiZARD » Mon Jul 21, 2003 2:08 am

Hi!
first:
Code: Select all
$username="username";
$password="pass";
$database="dbname";
$conn = mysql_connect("localhost",$username,$password) or die( "Unable to connect: " . $my_sqlerror());
$dbase=mysql_select_db($database) or die( "Unable to select database: " . $my_sqlerror());

$query= "SELECT * FROM `search`";
$result=mysql_query($query);
$num=mysql_numrows($result);

and second:
Code: Select all
$id=mysql_result($result,$i,"id");
//`words`: where formatted text goes
$query2 = "UPDATE `search` WHERE `id` = " . $id . " SET `words` = " . $text;
mysql_query($query2);
   
   ++$i;
}
   
mysql_close();
User avatar
WiZARD
Moderator
Moderator
 
Posts: 1257
Joined: Thu Jun 20, 2002 10:14 pm
Location: Ukraine, Crimea, Simferopol


Return to PHP General

Who is online

Users browsing this forum: No registered users and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron