2016-07-09 35 views
-1

我是編程初學者。我正在學習PHP。我想要做的是在PHP中爲我的項目編寫一個搜索腳本。當我嘗試通過MySQL數據庫搜索它給我一個錯誤:PHP搜索代碼不輸出結果結果

Notice: Undefined variable: output in C:\xampp\htdocs\search.php on line 2

我已經檢查了腳本上的一切,不能看到問題。我編碼錯了嗎?

我已經在論壇上檢查了所有可能的問題,涉及到我的問題,他們似乎沒有給我留下我需要的答案。請幫忙。

這是HTML腳本的輸入:

<form action="search.php" method="post" id="search"> 
     <div id="searchfield_div"> 
     <input name="search" id="searchfield" type="text" placeholder="What you looking for?"> 
     <input id="delete_search_button" name="delete button" type="button" value="X"> 
     <input id="search_button" name="search button" type="submit" value="Search"> 
     </div> 
    </form> 

,這是我的PHP腳本:

<?php 
//connection script 
$db_host = "localhost"; 
$db_user = "root"; 
$db_pass = ""; 
$db_name = "liquihub"; 
$output = ''; 

@mysqli_connect("$db_host","$db_user","$db_pass","$db_name") or die("could  not connect"); 

//collection script 
if (isset($_POST['search'])) { 
$searchq = $_POST['search']; 
$searchq = preg_replace("#[^0-9a-z]#i","", $searchq); 

$query = mysql_query("SELECT * FROM beverage_db WHERE name LIKE '%$searchq%' OR price LIKE '%$searchq%' OR type LIKE '%$searchq%'") or die ("could not search"); 
$count = mysql_num_rows($query); 
if ($count == 0) { 
    $output = 'We not stocking this particular item at present'; 
}else{ 
    while($row = mysql_fetch_array($query)) { 
     $bevname = $row['name']; 
     $bevprice = $row['price']; 
     $bevtype = $row['type']; 
     $bevid = $row['id']; 

     $output .= '<div>'.$bevname.' '.$bevprice.' '.$bevtype.'</div>'; 
     } 
    } 
} 
?> 

說的意思把結果在不同的頁面輸出腳本:

<?php print("$output");?> 
+0

你不能混用'mysqli_'和'mysql_'功能。你應該堅持'mysqli_' – tkausl

+0

我已經將它們全部更改爲mysqli_,但它仍然是相同的問題 – user2938948

+1

可能重複的[PHP:「注意:未定義的變量」和「注意:未定義的索引」](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – matiaslauriti

回答

0

你沒有問題,你得到的是一個警告不是錯誤。你的問題是重複的。 Here

+0

我試過這個解決方案,但仍然是同樣的事情。我需要改變什麼才能使其輸出結果?因爲輸出頁面上沒有任何內容可以免除該通知 – user2938948

+0

如果您正在更改頁面,則不會爲保存會話或使用include而保存'$ output'。並且不要'print(「$ output」)'直接'echo $ output' – matiaslauriti

+0

我會在哪裏放會話?我如何寫出我的代碼? – user2938948