匹配问题

发布于 2024-10-08 05:13:45 字数 994 浏览 2 评论 0原文

我试图用 php 和 mysql 制作一个搜索引擎,我再次使用匹配。

然而我有一个问题(可能是语法)让我发疯。

这是代码:

<?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"]; 
} 
}
?> 

这是多个单词搜索后的错误:

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

字段设置为全文......

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不喜欢何必死缠烂打 2024-10-15 05:13:45

该警告意味着您的查询失败。如果查询失败,mysql_query() 返回布尔值 FALSE,您可以使用 mysql_error() 检索错误消息:

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

您的代码假设查询成功并尝试获取行来自该错误值,该值不是有效的结果句柄。假设查询会成功是不好的做法。即使查询字符串在语法上是有效的,也可能会通过许多其他方式发生失败,并且您应该在每一步检查是否成功(或失败)。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文