PHP,Catchable致命错误:类mysqli的对象无法转换为字符串

时间:2016-05-14 21:03:18

标签: php html mysql mysqli

可捕获的致命错误:类mysqli的对象无法转换为字符串...

这是我的index.php

<?php 
session_start();
include("db.php");

 ?>

 <html>
 <head>
    <title>BLog</title>
 </head>
 <body>
     <?php 
        require("nbbc/nbbc.php");

        $bbcode = new BBCode;
        if (!$bbcode){
    die($con->error);
}

        $sql = "SELECT * FROM post ORDER BY id DESC";

        $res = mysqli_query($db, $sql) or die (mysqli_error());

        $posts = "";

        if(mysqli_num_rows($res) > 0) {
            while($row = mysqli_fetch_assoc($res)) {
                $id = $row['id'];
                $title = $row['title'];
                $content = $row['content'];
                $date = $row['date'];

                $admin = "<div><a href='del_post.php?   pid=$id'>Delete</a>&nbsp;<a href='edit_post.php?pid=$id'>Edit</a></div>";

                $output = $bbcode -> Parse($content);

                $posts = "<div><h2><a href='view_post.php?pid=$id'>$title</a></h2><h3>$date</h3><p>$output</p>$admin</div>";
            }
            echo $posts->fetch_object()->memTotal;
        }else {
            echo "There are no posts to display"."<br>";
            echo "<a href='post.php'>POST!</a>";
        }
      ?>
 </body>
 </html>

这是post.php:

<?php 
    session_start();
    include("db.php");

    if(isset($_POST['post'])){
        $title = strip_tags($_POST['title']);
        $content = strip_tags($_POST['content']);

        $title = mysqli_real_escape_string($db, $title);
        $content = mysqli_real_escape_string($db, $content);

        $data = date('l jS \of F Y h:i:s A');

        $sql="INSERT INTO post VALUES(null,'$title','$content','$data')";

        if($title == ""|| $content == ""){
            echo "Please complete your post";
            return;
        };

        mysqli_query("$db, $sql");

        header("Location: index.php");

    };
 ?>

 <html>
 <head>
    <title>Blog - Post</title>
 </head>
 <body>
    <form action="post.php" method="post" enctype="multipart/form-data">
        <input placeholder="Title" name="title" type="text" autofocus size="48"><br /><br />
        <textarea placeholder="Content" name="content" rows="20" cols="50"></textarea><br />
        <input name="post" type="submit" value="Post">
    </form>
 </body>
 </html>

这是db.php:

<?php 
    $db=mysqli_connect("localhost","root","","test");

 ?>

它给出了这种错误“可捕获的致命错误:类mysqli的对象无法在第21行的C:\ xampp \ htdocs \ web \ blog \ post.php中转换为字符串” 有什么问题?

2 个答案:

答案 0 :(得分:1)

错误在这一行:

mysqli_query("$db, $sql");

将其更改为以下内容:

mysqli_query($db, $sql);

mysqli_query功能的签名:

  

混合mysqli_query(mysqli $ link,string $ query [,int $ resultmode =   MYSQLI_STORE_RESULT])

http://php.net/manual/ru/mysqli.query.php

答案 1 :(得分:0)

Catchable fatal error: Object of class mysqli could not be converted to string in...

通过引用将mysqli_query参数作为字符串传递。

所以改变这行代码

 mysqli_query("$db, $sql"); // there is no need to use quotes here

使用正确的

 mysqli_query($db, $sql);