这是一段用来分词并进行词性匹配的程序段,其中数据库表cixing有word和cx两个字段,用的是PHP+APACH+MYSQL  
为什么有时候运行很慢,有时候又可以,关系到毕业设计,先谢谢各位大侠了  $temp="唐朝的皇帝是谁";  $str_len=strlen($temp);//输入字符串的总长度  
if($str_len!=0)//若字符串不为空  
{  
$str=array();//初始化一个数组  
$pos=0;//当前位置  
$len=2;  
$att_str=array();//初始化一个数组  
while($pos<$str_len)  
{  
$str_tmp=substr($temp,$pos,$len);//取第一个字“唐”  //到数据库查询$str_tmp  $hd=mysql_connect($server_hostname,$server_username,$server_password) ;  
$dbname=mysql_select_db("history",$hd);  
$query="select * from cixing where word='$str_tmp' ";  
$result=mysql_query($query);  
mysql_close($hd);  
$re=mysql_fetch_row($result);  if($re[0]==$str_tmp){  $Word=$re[0]; //返回单词  
$Cixing=$re[1]; //返回词性  $pos=$pos+strlen($str_tmp);  
$len=2;  array_push($str,$Word,$Cixing);//将单词和词性入栈$str  echo "<br>";  
$i++;  
$j++;  
}  
else{  $len=$len+2;  

 

解决方案 »

  1.   

    $hd=mysql_connect($server_hostname,$server_username,$server_password) ;  将这段放到while外面去试试代码还有很多地方要优化
      

  2.   

    你这样的mysql全文查询是这样的,你可以优化一下你的mysql
      

  3.   

    我现在连localhost都打不开了,但上网打开其他页面都没问题,这到底是怎么了,可不可以帮帮我啊!!
      

  4.   

    $pos=0;//当前位置
    $len=2;
    $att_str=array();//初始化一个数组
    while($pos<$str_len) //死循环
    =======================
    static $pos=0;//当前位置
    $len=2;
    $att_str=array();//初始化一个数组
    while($pos<$str_len) 
    ========================================