2013-11-23 32 views
0

我在我的網站中有一個數據表,我想添加可能基於條件的搜索功能,例如名稱,類型和日期,因爲表中的數據是關於事件的。使用SEARCH功能過濾現有表時出錯

我需要的結果在同一個表進行過濾,但我得到這個錯誤:

Catchable fatal error: Object of class mysqli_result could not be converted to string

這裏是我的表碼:

<?php 

    if(isset($_POST['searchbox']) && $_POST['searchbox'] !=""){ 
     $search=preg_replace('#[^a-z 0-9?!]#i','',$_POST['searchbox']); 


     $user="admin"; 
     $pass="neehahs"; 
     $host="localhost"; 
     $db_name="eventregisteration"; 

     $con=mysqli_connect($host, $user, $pass, $db_name); 
      if(mysqli_connect_errno($con)){ 
      echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
      } 
      if($_POST['select']=="type"){ 
     $sqlcommand="SELECT * FROM eventform WHERE event_type LIKE '%$search%'";  


     } 
     elseif($_POST['select']=="name"){ 
     $sqlcommand="SELECT * FROM eventform WHERE event_name LIKE '%$search%'";   


     } 
      $sqldata=mysqli_query($con,$sqlcommand) 
      or die("Error Getting Data"); 

      $count=mysqli_num_rows($sqldata); 
      if($count>1){ 
       while($row=mysqli_fetch_array($sqldata)){ 
      echo "<tr align=center><td>"; 
       echo $row['event_code']; 

       echo "</td><td>"; 
       echo $row['event_name']; 
       echo "</td><td>"; 

       echo $row['event_type']; 
       echo "</td><td>"; 

       echo $row['event_level']; 
       echo "</td><td>"; 

       echo $row['start_date']; 
       echo "</td><td>"; 

       echo $row['end_date']; 
       echo "</td><td>"; 

       echo $row['points']; 
       echo "</td><td>"; 

       echo $row['pic']; 
       echo "</td><td>"; 

       echo $row['video']; 
       echo "</td><td>"; 

       echo $row['description']; 
       echo "</td></tr>"; 

       } 


      }else{ 
       $search_output="<hr/>0 Results for<strong>$sqldata</strong><hr/>$sqlcommand"; 

    } 
    } 

    ?> 

回答

1

問題是這一行:

$search_output="<hr/>0 Results for<strong>$sqldata</strong><hr/>$sqlcommand"; 

mysqli_query返回一個mysqli_result對象。連接形成$search_output PHP嘗試將$sqldata作爲一個字符串進行投射,這根本無法完成。這就是錯誤所指的。

考慮到上下文(在消息中並使用<strong>標籤封裝),可能您的意思是使用的是$search而不是$sqldata


我還考慮到該消息消除$sqlcommand,除非是你當前調試會話的目的,否則它揭示了你的內部這將暗示對一個安全漏洞的重要信息。