我找到了一个非常好的搜索查询here,但是此搜索查询使用了mysql database
,现在我正在尝试将此代码调整为SQLite3 database
但是使用我当前的代码我得到了这个错误
致命错误:在第79行的C:\ xampp \ htdocs \ xport \ searchtext.php中的非对象上调用成员函数fetchArray()
以下是我当前的代码
<?php
require_once ("db.php");
$db = new MyDB();
$with_any_one_of = "";
$with_the_exact_of = "";
$without = "";
$starts_with = "";
$search_in = "";
$advance_search_submit = "";
$queryCondition = "";
if(!empty($_POST["search"])) {
$advance_search_submit = $_POST["advance_search_submit"];
foreach($_POST["search"] as $k=>$v){
if(!empty($v)) {
$queryCases = array("with_any_one_of","with_the_exact_of","without","starts_with");
if(in_array($k,$queryCases)) {
if(!empty($queryCondition)) {
$queryCondition .= " AND ";
} else {
$queryCondition .= " WHERE ";
}
}
switch($k) {
case "with_any_one_of":
$with_any_one_of = $v;
$wordsAry = explode(" ", $v);
$wordsCount = count($wordsAry);
for($i=0;$i<$wordsCount;$i++) {
if(!empty($_POST["search"]["search_in"])) {
$queryCondition .= $_POST["search"]["search_in"] . " LIKE '%" . $wordsAry[$i] . "%'";
} else {
$queryCondition .= "question LIKE '" . $wordsAry[$i] . "%' OR answer LIKE '" . $wordsAry[$i] . "%'";
}
if($i!=$wordsCount-1) {
$queryCondition .= " OR ";
}
}
break;
case "with_the_exact_of":
$with_the_exact_of = $v;
if(!empty($_POST["search"]["search_in"])) {
$queryCondition .= $_POST["search"]["search_in"] . " LIKE '%" . $v . "%'";
} else {
$queryCondition .= "question LIKE '%" . $v . "%' OR answer LIKE '%" . $v . "%'";
}
break;
case "without":
$without = $v;
if(!empty($_POST["search"]["search_in"])) {
$queryCondition .= $_POST["search"]["search_in"] . " NOT LIKE '%" . $v . "%'";
} else {
$queryCondition .= "question NOT LIKE '%" . $v . "%' AND answer NOT LIKE '%" . $v . "%'";
}
break;
case "starts_with":
$starts_with = $v;
if(!empty($_POST["search"]["search_in"])) {
$queryCondition .= $_POST["search"]["search_in"] . " LIKE '" . $v . "%'";
} else {
$queryCondition .= "question LIKE '" . $v . "%' OR answer LIKE '" . $v . "%'";
}
break;
case "search_in":
$search_in = $_POST["search"]["search_in"];
break;
}
}
}
}
$orderby = " ORDER BY quiz_id desc";
$sql = "SELECT * FROM questions " . $queryCondition;
$result = $db->exec($sql);
if($result)
{
echo "Good";
while ($row = $result->fetchArray(SQLITE3_ASSOC))
{
$question = $row['question'];
echo $question;
}
}
else
{
echo "No results found";
}
?>
<form name="frmSearch" method="post" action="searchtext.php">
<input type="hidden" id="advance_search_submit" name="advance_search_submit" value="<?php echo $advance_search_submit; ?>">
<div class="search-box">
<label class="search-label">With Any One of the Words:</label>
<div>
<input type="text" name="search[with_any_one_of]" class="demoInputBox" value="<?php echo $with_any_one_of; ?>" />
<span id="advance_search_link" onClick="showHideAdvanceSearch()">Advance Search</span>
</div>
<div id="advanced-search-box" <?php if(empty($advance_search_submit)) { ?>style="display:none;"<?php } ?>>
<label class="search-label">With the Exact String:</label>
<div>
<input type="text" name="search[with_the_exact_of]" id="with_the_exact_of" class="demoInputBox" value="<?php echo $with_the_exact_of; ?>" />
</div>
<label class="search-label">Without:</label>
<div>
<input type="text" name="search[without]" id="without" class="demoInputBox" value="<?php echo $without; ?>" />
</div>
<label class="search-label">Starts With:</label>
<div>
<input type="text" name="search[starts_with]" id="starts_with" class="demoInputBox" value="<?php echo $starts_with; ?>" />
</div>
<label class="search-label">Search Keywords in:</label>
<div>
<select name="search[search_in]" id="search_in" class="demoInputBox">
<option value="">Select Column</option>
<option value="title" <?php if($search_in=="title") { echo "selected"; } ?>>Title</option>
<option value="description" <?php if($search_in=="description") { echo "selected"; } ?>>Description</option>
</select>
</div>
</div>
<div>
<input type="submit" name="go" class="btnSearch" value="Search">
</div>
</div>
</form>
请任何帮助将不胜感激。提前谢谢。
答案 0 :(得分:0)
有几种不同的方法可以做到这一点。您可以尝试使用下面提到的方法 -
$query->fetch(SQLITE_ASSOC)
,这是面向对象的方法。现在您正在遵循这种方法,使用这种方法更改您的代码,希望它能够正常工作。
另一种方法可以是像这段代码while ($entry = sqlite_fetch_array($query, SQLITE_ASSOC)) {