首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么我要获得mysqli类的错误对象不能转换为string

问为什么我要获得mysqli类的错误对象不能转换为string
EN

Stack Overflow用户
提问于 2016-10-10 11:02:23
回答 1查看 124关注 0票数 0

这是我的完整代码:

代码语言:javascript
复制
<?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类的对象无法在第29行的C:\wamp\www\hari\journalkart\search.php中转换为字符串。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-10-10 11:04:58

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

修改查询:

代码语言:javascript
复制
<?php
$raw_results = mysqli_query($con,"SELECT * FROM vendor1
        WHERE (title LIKE '%".$query."%') OR (publisher LIKE '%".$query."%')");
?>

,你的代码怎么了?

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

代码语言:javascript
复制
mysqli_query("$con,"SELECT QUERY");

还有一点,如果您使用的是*扩展,而不是为什么要使用mysql_error(),则不能将这两个扩展混合在一起。

如果要检查mysqli错误,则可以将mysqli_error()函数用作:

代码语言:javascript
复制
mysqli_error($con);

建议使用http://php.net/manual/en/pdo.prepared-statements.php,这将节省您的代码与SQL注入。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39956787

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档