开发者

Problems with match against

开发者 https://www.devze.com 2023-01-31 17:54 出处:网络
im trying to make a search engine with php and mysql and im using match agains. However im having a problem (probably syntax) that is driving me crazy.

im trying to make a search engine with php and mysql and im using match agains.

However im having a problem (probably syntax) that is driving me crazy.

Here is the code:

<?php 
$busqueda= $_GET["words"];
require("conectdb.php");

if ($busqueda<>''){ 

   $trozos=explode(" ",$busqueda); 
   $numero=count($trozos); 
  if ($numero==1) { 

   $cadbusca="SELECT * FROM post WHERE contenido LIKE '%$busqueda%' OR titulo LIKE '%$开发者_开发技巧busqueda%'"; 
  } elseif ($numero>1) { 

    $cadbusca="SELECT * , MATCH ( 'titulo', 'contenido' ) AGAINST ( '$busqueda' )  AS Score FROM post WHERE MATCH ( 'titulo', 'contenido' ) AGAINST ( '$busqueda' ) ORDER BY Score DESC";

} 

$result=(mysql_query($cadbusca)); 
while($info = mysql_fetch_array($result))
{ 
  echo $info["id"]." ".$info["titulo"]." ".$info["contenido"]; 
} 
}
?> 

here is the error after more than one word search:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home1/foodbook/public_html/search/wordsearch.php on line 19

The fields are set to fulltext....

Thx


The warning means that your query failed. If a query fails, mysql_query() returns boolean FALSE and you can retrieve the error message with mysql_error():

$res = mysql_query($cadbusca);
if ($res === FALSE) {
      die("Query failed: " . mysql_error());
}

Your code is assuming the query succeeded and attempts to fetch a row from that false value, which is not a valid result handle. It is bad practice to assume a query will succeed. Even if the query string is syntactically valid, there's many many many other ways failure can occur, and you should check for success (or failure) at every step.

0

精彩评论

暂无评论...
验证码 换一张
取 消