我将页面保存在 mysql db 中,其中一个字段是每个页面的标签数组,希望对站点搜索有所帮助。
我收到一个由我的电话引起的错误...
$results = $db->select('pages','','','name',array('name', 'DESC'),'10',array('tags', '%' .$word. '%'));
(选择作为 -- 'table','where','bind for where query match','fields','orderby array','limit',where/like array')
我认为问题在于“标签”字段是一个数组。解决此问题的最佳方法是什么?如有必要,我将在查询后将每个结果拉起来:
//if we got something through $_POST
if (isset($_GET['search'])) {
// here you would normally include some database connection
require_once('../config/dbconfig.php');
// never trust what user wrote! We must ALWAYS sanitize user input
//$word = mysql_real_escape_string($_POST['search']);
$word = htmlentities($_GET['search']);
// build your search query to the database
//$sql = "SELECT title, url FROM pages WHERE content LIKE '%" . $word . "%' ORDER BY title LIMIT 10";
$results = $db->select('pages','','','*',array('name', 'DESC'),'10',array('tags', '%' .$word. '%'));
// get results
if (count($results) > 0) {
$end_result = '';
echo '<ul>';
foreach($results as $row) {
$bold = '<span class="found">' .$word. '</span>';
$end_result .= '<li>' .str_ireplace($word, $bold, $row['title']). '</li>';
}
//echo $end_result. '</ul>';
}else {
//echo '<ul><li>No results found</li></ul>';
}
var_dump($results);
exit;
}
它说错误在我的foreach中:
警告:为 foreach() 提供的参数无效
到目前为止,每次都是因为我之前的查询。每一次。
我搜索并没有看到任何类似的东西,如果我错过了,我很抱歉。我也累了,所以如果我错过了一些细节,请告诉我,我会发布它们。谢谢!
这就是选择在运行之前的处理方式。
public function select($table, $where="", $bind="", $fields="*", $order="", $limit="", $like="") {
$sql = "SELECT " . $fields . " FROM " . $table;
if(!empty($where)) {
$sql .= " WHERE " . $where;
}
if (!empty($order)) {
$sql .= " ORDER BY " . $order[0] . " " . $order[1];
}
if (!empty($limit) && is_array($limit)) {
$sql .= " LIMIT " . $limit[0] . " " . $limit[1];
}
if (!empty($limit)) {
$sql .= " LIMIT " . $limit;
}
if (!empty($like)) {
$sql .= " WHERE " .$like[0]. " LIKE " . $like[1];
}
$sql .= ";";
//var_dump($sql);
//var_dump($bind);
//exit;
return $this->run($sql, $bind);
}
public function run($sql, $bind="") {
$this->sql = trim($sql);
$this->bind = $this->cleanup($bind);
$this->error = "";
try {
$pdostmt = $this->prepare($this->sql);
if($pdostmt->execute($this->bind) !== false) {
if(preg_match("/^(" . implode("|", array("select", "describe", "pragma")) . ") /i", $this->sql))
return $pdostmt->fetchAll(PDO::FETCH_ASSOC);
elseif(preg_match("/^(" . implode("|", array("delete", "insert", "update")) . ") /i", $this->sql))
return $pdostmt->rowCount();
}
} catch (PDOException $e) {
$this->error = $e->getMessage();
$this->debug();
return false;
}
}
(切换到 $_GET b/c 搜索正在使用 AJAX,并且不想更改一堆东西只是为了调用它,所以直接在带有 url 的页面中进行。)
nm 在我刚刚删除的那个查询编辑上,第一次是正确的......累对不起......