这是我的完整代码:
<?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 >
$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类的对象无法在第29行的C:\wamp\www\hari\journalkart\search.php中转换为字符串。
发布于 2016-10-10 11:04:58
是的,这是真的“mysqli类的对象不能转换为字符串”:
修改查询:
<?php
$raw_results = mysqli_query($con,"SELECT * FROM vendor1
WHERE (title LIKE '%".$query."%') OR (publisher LIKE '%".$query."%')");
?>,你的代码怎么了?
在字符串中使用整个查询和连接:
mysqli_query("$con,"SELECT QUERY");还有一点,如果您使用的是*扩展,而不是为什么要使用mysql_error(),则不能将这两个扩展混合在一起。
如果要检查mysqli错误,则可以将mysqli_error()函数用作:
mysqli_error($con);建议使用http://php.net/manual/en/pdo.prepared-statements.php,这将节省您的代码与SQL注入。
https://stackoverflow.com/questions/39956787
复制相似问题