2016-10-10 88 views
0

这是我的完整代码:为什么我收到类的mysqli的这个错误对象无法转换为字符串

<?php 
$query = $_GET['query']; 
// gets value sent over search form 

$min_length = 3; 
// you can set minimum length of the query if you want 

if(strlen($query) >= $min_length){ // if query length is more or equal minimum length then 

    $query = htmlspecialchars($query); 
    // changes characters used in html to their equivalents, for example: < to &gt; 

    $query = mysqli_real_escape_string($con, $query); 
    // makes sure nobody uses SQL injection 

    $raw_results = mysqli_query("$con, SELECT * FROM vendor1 
     WHERE (title LIKE '%".$query."%') OR (publisher LIKE '%".$query."%')") or die(mysql_error()); 

    // * means that it selects all fields, you can also write: `id`, `title`, `text` 
    // articles is the name of our table 

    // '%$query%' is what we're looking for, % means anything, for example if $query is Hello 
    // it will match "hello", "Hello man", "gogohello", if you want exact match use `title`='$query' 
    // or if you want to match just full word so "gogohello" is out use '% $query %' ...OR ... '$query %' ... OR ... '% $query' 

    if(mysqli_num_rows($raw_results) > 0){ // if one or more rows are returned do following 

     while($results = mysqli_fetch_array($raw_results)){ 
     // $results = mysqli_fetch_array($raw_results) puts data from database into array, while it's valid it does the loop 

      echo "<p><h3>".$results['title']."</h3>".$results['full_set']." ".$results['issn']." ".$results['publisher']."</p>"; 
      // posts results gotten from database(title and text) you can also show id ($results['id']) 
     } 

    } 
    else{ // if there is no matching rows do following 
     echo "No results"; 
    } 

} 
else{ // if query length is less than minimum 
    echo "Minimum length is ".$min_length; 
} 
?> 

这是我收到的错误: 开捕致命错误:类mysqli的可能的对象不能转换为字符串在C:\ WAMP \ WWW \哈利\ journalkart \ search.php中上线29

+0

你不能混合和匹配mysql_ *和mysqli_ * apis。 –

回答

0

是的,这是真正的 “类对象的mysqli不能转换成字符串”:

修改查询:

<?php 
$raw_results = mysqli_query($con,"SELECT * FROM vendor1 
     WHERE (title LIKE '%".$query."%') OR (publisher LIKE '%".$query."%')"); 
?> 

你的代码有什么问题?

您使用整个查询和连接字符串中的:

mysqli_query("$con,"SELECT QUERY"); 

还有一件事,如果你正在使用mysqli_*扩展你为什么要使用mysql_error()比,你不能同时扩展混合在一起。

如果你想检查mysqli的错误比你可以使用mysqli_error()功能:

mysqli_error($con); 

它的建议,使用Prepared Statement,这将节省与SQL注入你的代码。

相关问题