我正在創建一個動態PHP網站,其中包括網站上的所有內容,包括導航都是動態生成的。我創建了一個測試頁面(test.php),因爲我是PHP新手,並且在執行多個查詢時還有一些問題,還有一個準備好的語句,因爲我在頁面上有一個搜索表單。基本上我想要做的是,在SQL中,爲父級= 0的頂級導航項創建查詢,並且還基於搜索在另一個表中搜索項目。這兩個查詢是無關的。在我的身體裏,我有導航顯示,我想嵌套另一個查詢,然後搜索導航項目與父母等於頂級菜單項的當前ID。這是我的文檔的頂部的SQL:MySQLi執行多個不相關的查詢並執行準備好的語句
try {
//db connection is here
$sql1 = 'SELECT * FROM menu_items WHERE item_parent = 0';
$result1 = $db->query($sql1);
if (isset($_GET['search'])) {
$sqlSearch = "SELECT id, css_body, page_title, meta_description, meta_keywords, heading, details, layout
FROM content
WHERE (details LIKE ? OR heading LIKE ?)";
$stmt = $db->stmt_init();
if (!$stmt->prepare($sqlSearch)) {
$error = $stmt->error;
} else {
$var1 = '%' . $_GET['searchterm'] . '%';
$stmt->bind_param('ss', $var1, $var1);
$stmt->execute();
$stmt->bind_result($id, $css_body, $page_title, $meta_description, $meta_keywords, $heading, $details, $layout);
}
}
} catch (Exception $e) {
$error = $e->getMessage();
}
在我的身上:
<ul>
<?php while ($row = $result1->fetch_assoc()) { ?>
<li><?php echo $row["item_title"]; ?>
<?php $sql2 = 'SELECT * FROM menu_items WHERE item_parent = ' . $row["item_id"];
$result2 = $db->query($sql2);
if (isset($result2)) { ?>
<ul>
<?php while ($row2 = $result2->fetch_assoc()) { ?>
<li><?php echo $row2["item_title"]; ?></li>
<?php } ?>
</ul>
<?php } ?>
</li>
<?php } ?>
</ul>
與該代碼,如果我只是直奔test.php的頁面,而無需進行搜索,一切都顯示正常。例如,頁面看起來像:
<ul>
<li>Page 1</li>
<ul>
<li>Sub Page 1</li>
<li>Sub Page 2</li>
</ul>
<li>Page 2</li>
</ul>
但是,如果我再提交頁面上的搜索表單(它提交到同一頁面,網址看起來可能就像test.php的搜索關鍵詞=我+搜索&搜索? =去),然後我得到一個錯誤。該頁面將顯示第一個LI,但當它遇到第二個查詢時,它會完全停止。所以它看起來像:
<ul>
<li>Page 1</li>
如果我刪除一切爲了準備好的聲明中,「子導航」 /嵌套查詢工作正常。如果我刪除了menu_items的SQL/php,那麼準備好的語句工作正常。這個問題是當我把所有東西放在一起時。如果有人能解釋我可能做錯了什麼,我將不勝感激。我是新來的準備語句和麪向對象的PHP。在Lynda.com上與David Powers一起觀看使用面向對象的PHP訪問數據庫之後,我學到了上述所有知識。
這裏是整個頁面代碼:
<?php
try {
require_once '_includes/mysqli-connect.php';
$sql1 = 'SELECT * FROM menu_items WHERE item_parent = 0';
$result1 = $db->query($sql1);
if (isset($_GET['search'])) {
$sqlSearch = "SELECT heading, details FROM content WHERE (details LIKE ? OR heading LIKE ?)";
$stmt = $db->stmt_init();
if (!$stmt->prepare($sqlSearch)) {
$error = $stmt->error;
} else {
$var1 = '%' . $_GET['searchterm'] . '%';
$stmt->bind_param('ss', $var1, $var1);
$stmt->execute();
$stmt->bind_result($heading, $details);
}
}
} catch (Exception $e) {
$error = $e->getMessage();
}
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Test</title>
</head>
<body>
<?php if (isset($error)) {
echo "<p>$error</p>";
}
?>
<ul>
<?php while ($row = $result1->fetch_assoc()) { ?>
<li><?php echo $row["item_title"]; ?>
<?php
$sql2 = 'SELECT * FROM menu_items WHERE item_parent = ' .$row["item_id"];
$result2 = $db->query($sql2);
if (isset($error)) {
echo "<p>$error</p>";
}
if (isset($result2)) { ?>
<ul>
<?php while ($row2 = $result2->fetch_assoc()) { ?>
<li><?php echo $row2["item_title"]; ?></li>
<?php } ?>
</ul>
<?php }
?>
</li>
<?php } ?>
</ul>
<!-- Search form and results below -->
<form method="get" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<p>
<label for="searchterm">Enter a name or part of one:</label>
<input type="search" name="searchterm" id="searchterm"> <input type="submit" name="search" value="Go"></p>
</form>
<?php
if (isset($error)) {
echo "<p>$error</p>";
}
$stmt->store_result();
$numrows = $stmt->num_rows;
if ($numrows) {
echo "<p>Total results found: ". $numrows;
} else {
echo "No Results";
}
while ($stmt->fetch()) {
echo "<p>Heading: " . $heading . "</p>";
echo $details;
echo "<hr/>";
}
?>
</body>
</html>
<?php
$result1->free();
$stmt->free_result();
if (isset($db)) {
$db->close();
} ?>
您在結束之前有錯誤嗎?什麼時候需要?>另外,嘗試在你的bind-> param語句之上移動你的var1聲明。 – Jim 2014-09-29 18:36:39
我在這裏編輯代碼時必須意外刪除>,但它在我的實際文件代碼中,所以這不是問題。按照您的建議,我提出了var1聲明,但問題依然存在。 – user2762748 2014-09-29 18:43:57
我沒有看到你的'if(isset($ _ GET ['search'])){'正在關閉任何地方。你應該考慮把你的代碼分解成函數。閱讀現在的方式很難。此外,它看起來並不像你實際上正在基於搜索輸入做任何過濾。 – 2014-09-29 18:46:57