如何在 php fwrite 中有一个 mysqli stament

How to have a mysqli stament inside an php fwrite

我正在尝试使用 php fopen 和 fwrite 创建一个新页面。在 fwrite 中,代码应该是一个用于简单页面计数器的 mysqli 语句。 fwrite 和 fopen 正常工作并创建一个新页面 html(例如)当我使用 mysqli 语句时它不起作用。这是错误:

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

这是代码

$page = "

<?php

$con = new mysqli('localhost', 'mathswi3_data', '...',
'mathswi3_submits');

if ($mysqli->connect_error) {
die('Errr : ('. $mysqli->connect_errno .') '. $mysqli->connect_error);
}


if (!mysqli_query($con, \"UPDATE submits SET views=views + 1 
WHERE url    = $nurl\"));
{
echo(\"Error description: \" . mysqli_error($con));
}
?>";

fwrite($fh, $page);
$filname = "tools/scripts/toolid.php";

fclose($fh);
exit();
?> 

唯一的错误就是上面所说的那个。有人知道如何在 fwrite 中使用 mysqli stament吗 "cant be converted into a string..."

这里让您措手不及的功能叫做 string interpolation

$foo = "test";
$bar = "this is a $foo";

var_dump($bar); // "this is a test"

用双引号创建的字符串可以包含变量。这些变量将在创建字符串时将它们的值插入到字符串中。

当您创建 $page 时,您使用的是双引号。在到达 $mysqli 之前一切都很好,此时 php 尝试将其转换为字符串,以便它可以适合此 $page 变量。

您需要 escape 美元符号像这样带有斜杠。

<?php

$con = new mysqli('localhost', 'mathswi3_data', '...',
'mathswi3_submits');

if ($mysqli->connect_error) {
die('Errr : ('. $mysqli->connect_errno .') '. $mysqli->connect_error);
}