| Author |
The search.. |
Gaby de Wilde
Joined: Nov 14, 2021 Posts: 13 From: The land of cheese
| Posted: 2024-09-10 13:56  
The original mysql query for the search engine (after running it though a formatter)
Code:
|
$query = "SELECT u.user_id,f.forum_id, p.topic_id, u.username, p.post_time,t.topic_title,f.forum_name
FROM posts p, posts_text pt, users u, forums f,topics t";
if(isset($term) && $term != "")
{
$terms = split(" ",addslashes($term)); // Get all the words into an array
$addquery .= "(pt.post_text LIKE '%$terms[0]%'";
$subquery .= "(t.topic_title LIKE '%$terms[0]%'";
if($addterms=="any") // AND/OR relates to the ANY or ALL on Search Page
$andor = "OR";
else
$andor = "AND";
$size = sizeof($terms);
for($i=1;$i<$size;$i++) {
$addquery.=" $andor pt.post_text LIKE '%$terms[$i]%'";
$subquery.=" $andor t.topic_title LIKE '%$terms[$i]%'";
}
$addquery.=")";
$subquery.=")";
}
if(isset($forum) && $forum!="all")
{
if(isset($addquery)) {
$addquery .= " AND ";
$subquery .= " AND ";
}
$addquery .=" p.forum_id=$forum";
$subquery .=" p.forum_id=$forum";
}
if(isset($search_username)&&$search_username!="")
{
$search_username = addslashes($search_username);
if(!$result = mysql_query("SELECT user_id FROM users WHERE username='$search_username'",$db))
{
error_die("<font size=+1>An Error Occured</font><hr>phpBB was unable to query the forums database");
}
$row = @mysql_fetch_array($result);
if(!$row)
{
error_die("That user does not exist. Please go back and search again.");
}
$userid = $row[user_id];
if(isset($addquery)) {
$addquery.=" AND p.poster_id=$userid AND u.username='$search_username'";
$subquery.=" AND p.poster_id=$userid AND u.username='$search_username'";
}
else {
$addquery.=" p.poster_id=$userid AND u.username='$search_username'";
$subquery.=" p.poster_id=$userid AND u.username='$search_username'";
}
}
if(isset($addquery)) {
switch ($searchboth) {
case "both" :
$query .= " WHERE ( $subquery OR $addquery ) AND ";
break;
case "title" :
$query .= " WHERE ( $subquery ) AND ";
break;
case "text" :
$query .= " WHERE ( $addquery ) AND ";
break;
}
}
else
{
$query.=" WHERE ";
}
$query .= " p.post_id = pt.post_id
AND p.topic_id = t.topic_id
AND p.forum_id = f.forum_id
AND p.poster_id = u.user_id
AND f.forum_type != 1";
// 100100 bartvb Uncomment the following GROUP BY line to show matching topics instead of all matching posts.
// $query .= " GROUP BY t.topic_id";
$query .= " ORDER BY $sortby";
$query .= " LIMIT 200";
if(!$result = mysql_query($query,$db))
{
die("<font size=+1>An Error Occured</font><hr>phpBB was unable to query the forums database<BR>".mysql_error($db)."<BR>$query");
}
if(!$row = @mysql_fetch_array($result))
{
die($l_nomatches);
} |
|
 
|
Gaby de Wilde
Joined: Nov 14, 2021 Posts: 13 From: The land of cheese
| Posted: 2024-09-10 13:59  
The fixed version currently looks like this:
Code:
| $query = "
SELECT u.user_id, f.forum_id, p.topic_id, u.username, p.post_time, t.topic_title, f.forum_name
FROM posts p
JOIN posts_text pt ON p.post_id = pt.post_id
JOIN topics t ON p.topic_id = t.topic_id
JOIN forums f ON p.forum_id = f.forum_id
JOIN users u ON p.poster_id = u.user_id
WHERE 1=1 ";
$topic_title = [];
$post_text = [];
$params = [];
$params2 = [];
$param_type = "";
$param_type2 = "";
$andor = ($addterms=="any")?" AND ":" OR ";
if($search_username && $search_username!=""){
$query.=" AND p.poster_id=? AND u.username=? ";
$params[] = $userid;
$params[] = $search_username;
$param_type .= "is";
}
if($forum && $forum!="all"){
$query .=" AND p.forum_id=? ";
$params[] = $forum;
$param_type .= "i";
}
$query .= "AND f.forum_type != ?";
$params[] = 1;
$param_type .= "i";
if($term && $term != ""){
$terms = explode(" ",$term);
if(sizeof($terms) > 20){die("You may search 20 words at most. Please go back and search again.");}
foreach($term as $terms){
if($terms == ""){continue;}
if(strlen($terms) > 64){die("Words may not be longer than 64 characters. Please go back and search again.");}
$topic_title[] = " t.topic_title LIKE ? ";
$post_text[] = " pt.post_text LIKE ? ";
$params2[] = "%".$terms."%";
$param_type2 .= "s";
}
if($searchboth=="title"){
$query .= " AND ( ".implode($andor, $topic_title)." ) ";
}else if($searchboth=="text"){
$query .= " AND ( ".implode($andor, $post_text)." ) ";
}else{ // search both
$params2 = array_merge($params2, $params2);
$param_type2 = $param_type2.$param_type2;
$query .= " AND ( ( ".implode($andor, $topic_title)." ) OR ( ".implode($andor, $post_text)." ) ) ";
}
$params = array_merge($params,$params2);
$param_type .= $param_type2;
}
if($sortby){
if($sortby == "t.topic_title"){$query .= " ORDER BY t.topic_title";}
else if($sortby == "f.forum_name") {$query .= " ORDER BY f.forum_name";}
else if($sortby == "u.username") {$query .= " ORDER BY u.username";}
else{$query .= " ORDER BY p.post_time desc";}
}
$query .= " LIMIT 200";
$stmt = $db->prepare($query);
$stmt->bind_param($param_type, ...$params);
$result = $stmt->execute();
if (!$result){
die("Error executing query: " . $stmt->error);
} |
|
It worked first try which is rather suspicious. It might still have some technological challenges.
Ahhh, A bug was found. The paranoia was satisfied.
[ This Message was edited by: Gaby de Wilde on 2024-09-10 14:01 ]
[ This Message was edited by: Gaby de Wilde on 2024-09-10 16:01 ]
 
|
|