Chingiz Isaev Chingiz Isaev - 17 days ago 12
HTML Question

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

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

This is my 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>


and this is 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>


and this is db.php:

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

?>


It's gives this kind of error "Catchable fatal error: Object of class mysqli could not be converted to string in C:\xampp\htdocs\web\blog\post.php on line 21"
What's problem?

Answer

The error is on this line:

mysqli_query("$db, $sql");

Change it to the following:

mysqli_query($db, $sql);

The signature of mysqli_query function:

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

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