アプリ版:「スタンプのみでお礼する」機能のリリースについて

検索フォームを作成し、結果を別ページに表示したいと思っておりますが、うまくいかないので質問させて頂きます。

・状況
検索ワードが部分一致での結果は表示できるのですが、Yahooのように複数検索すると結果が表示されません。
また、検索ワードの前後に半角スペースや全角スペースなどを入れても同様です。

・ソース
検索フォーム側

<form id="form1" name="form1" method="get" action="search.php">
<label for="textfield"></label>
<input type="text" name="title" id="textfield" />
<input type="submit" name="button" id="button" value="検索" />
</form>

検索結果側(search.php)

<?php require_once('Connections/tm.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
if (PHP_VERSION < 6) {
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
}

$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}

$colname_search = "-1";
if (isset($_GET['title'])) {
$colname_search = $_GET['title'];
}
mysql_select_db($database_tm, $tm);
$query_search = sprintf("SELECT * FROM posts WHERE title LIKE %s ORDER BY title DESC", GetSQLValueString("%" . $colname_search . "%", "text"));
$search = mysql_query($query_search, $tm) or die(mysql_error());
$row_search = mysql_fetch_assoc($search);
$totalRows_search = mysql_num_rows($search);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "ttp://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="ttp://www.w3.org/1999/xhtml">
<head>
<meta ttp-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>無題ドキュメント</title>
</head>
<body>
<table width="725" border="1" cellspacing="0" cellpadding="0">
<tr>
<th width="120" scope="col">タイトル</th>
<th width="120" scope="col">内容</th>
<th width="120" scope="col">名前</th>
</tr>
<tr>
<td><?php echo $row_search['title']; ?></td>
<td><?php echo $row_search['message']; ?></td>
<td><?php echo $row_search['n_name']; ?></td>
</tr>
</table>
</body>
</html>
<?php
mysql_free_result($search);
?>

※検索結果側はテスト段階のため、まだ結果を一件のみ表示されるだけとなっています。

ネットで調べると
・全角スペースを半角に揃える
・orやandを使用する
らしき所までしか分かりませんでした。。。
出来れば詳しいソースや参考サイト等がありましたらご伝授をお願い致します。

A 回答 (1件)

複数の文字をand検索するのであれば



WHERE 1
AND `検索フィールド` LIKE '%検索ワード1%'
AND `検索フィールド` LIKE '%検索ワード2%'
AND `検索フィールド` LIKE '%検索ワード3%'
・・・

とします。OR検索なら
WHERE 1
(`検索フィールド` LIKE '%検索ワード1%'
OR `検索フィールド` LIKE '%検索ワード2%'
OR `検索フィールド` LIKE '%検索ワード3%'
・・・
)

みたいな感じです。

あとは受け取ったワードをそのロジックにあてはめるよう
PHP側でスペースで分割してループをすればいいでしょう
    • good
    • 0
この回答へのお礼

解決することが出来ました。
ありがとうございます

お礼日時:2012/05/04 13:37

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!

関連するカテゴリからQ&Aを探す