1. 程式人生 > >如何解決mid使用過程中出現的以下問題(無效的過程呼叫或引數: 'mid')

如何解決mid使用過程中出現的以下問題(無效的過程呼叫或引數: 'mid')

以下以原文程式來做一個分析,程式碼 :
set rs3=server.CreateObject("adodb.recordset")
 rs3.open "select id,wh from fgk order by len(wh) desc",conn,1,1  //取出資料
 while not rs3.eof
   title=rs3("wh")
   id=rs3("id")
 if instr(content,title)<>0 then   //查詢tilel的內容是否存在於content中
  position=instr(content,title)-2
  if mid(content,position,1)<>"3" then //目的實現判斷一下前面是否已經替換了,這樣可以避免短標題的內容重複替                                                                     //換,如果已經替換則無需替換

   title1="<a href='law_detail.asp?id="&id&"'><font color=red size=3>"&title&"</font></a>"
   content=replace(content,title,title1)
  end if
 end if   

然後實行這段程式碼,但總會出現錯誤:無效的過程呼叫或引數: 'mid' ,自己分析了一下是不是mid這個函式的功能使用是不是存在問題,仔細查了一遍文件並沒有找到任何不妥的地方,然後在if的判斷語句後面加入一個輸出語句,看錯誤出在哪裡,
if instr(content,title)<>0 then   //查詢tilel的內容是否存在於content中


  position=instr(content,title)-2
  response.write mid(content,position,1)
  if mid(content,position,1)<>"3" then //目的實現判斷一下前面是否已經替換了,這樣可以避免短標題的內容重複替                                                                     //換,如果已經替換則無需替換
   title1="<a href='law_detail.asp?id="&id&"'><font color=red size=3>"&title&"</font></a>"
   content=replace(content,title,title1)
  end if
再次執行,發現正常輸出幾行後再次出現這個錯誤:無效的過程呼叫或引數: 'mid'
既然前面可以正常輸出,說明程式是沒有問題的,有問題只能存在於內容本身,然後發現如果title為空的話,
if instr(content,title)<>0 then 這個條件是成立的,但這個時候 position=instr(content,title)-2 中的position可能
為負數,這樣就直接導致mid(content,position,1)出錯,因為在mid中position最小應該為0,於是在增加了一個判斷條件,如下
  if mid(content,position,1)<>"3" and   title<>"" then
再次執行程式,問題解決