gpt4 book ai didi

sql - SQL语法中的错误-收到此错误,无法查明错误的位置

转载 作者:行者123 更新时间:2023-12-03 08:14:49 24 4
gpt4 key购买 nike

我正在网站上建立搜索功能,但在查找SQL错误时遇到了一些麻烦。有四个SQL语句用于使用AND和OR关键字返回同一搜索的计数。第二组SQL语句返回AND和OR搜索的实际结果,该结果根据当前搜索的页码而受到限制。

下面,我将显示for和count和and搜索的SQL函数。
其他两个SQL语句的代码完全相同,仅使用OR而不是AND。

function _and_count($words) {

require_once("include/conn.inc.php");

$arraysize = count($words);

$and_count_sql = "SELECT COUNT(DISTINCT tg_id)
FROM tg_keywords
WHERE tg_keyword=";

if ($arraysize > 1) {
foreach($words as $word){
$count = 1;
if ($count == 1){
$and_count_sql .= "'".$word."' AND ";
}
elseif ($count < $arraysize){
$and_count_sql .= "tg_keyword='".$word."' AND ";
}
else {
$and_count_sql .= "'".$word."'";
}
$count++;
}

} elseif ($words == 1) {
$and_count_sql .= "'".$words."'";
}

$and_count_result = mysql_query($and_count_sql)
or die(mysql_error());

return $and_count_result;

}


function _search_and($words, $startpos, $endpos) {

require_once("include/conn.inc.php");

$arraysize = count($words);

$search_and_sql = "SELECT DISTINCT tg_id COUNT(*) AS nb
FROM tg_keywords
LEFT JOIN tg_info
ON tg_info.tg_id=tg_keywords.tg_id
WHERE tg_keyword=";

if ($arraysize > 1){
foreach($words as $word) {
$count = 1;
if ($count == 1 && $count < $arraysize) {
$search_and_sql .= "'".$word."' AND ";
} elseif ($count == 1 && $count == $arraysize) {
$search_and_sql .= "'".$word."'";
} elseif ($count < $arraysize) {
$search_and_sql .= "tg_keyword='".$word."' AND ";
} elseif ($arraysize == $count) {
$search_and_sql .= "'".$word."'";
}
$count++;
}
} elseif ($arraysize == 1) {
$search_and_sql .= "'".$words."'";
} else {
$search_and_sql .= "''";
}

$search_and_sql .= "GROUP BY tg_id
ORDER BY nb DESC, tg_info.date_added ASC
LIMIT ".$endpos." OFFSET ".$startpos;

$search_and_result = mysql_query($search_and_sql, $link)
or die(mysql_error());

$and_array = mysql_fetch_array($search_and_result);

return $and_array;


}

任何建议将不胜感激。
谢谢
阿奇

最佳答案

$ search_and_sql =“SELECT DISTINCT tg_id COUNT(*)AS nb

tg_id和Count之间缺少逗号。

展示生成的查询的优良作法,即。 echo $ search_and_sql;在mysql_query之前

关于sql - SQL语法中的错误-收到此错误,无法查明错误的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3636204/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com