如何在 php fwrite 中有一个 mysqli stament



我正在尝试使用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 状态,因为它"无法转换为字符串......"

你在这里被蒙蔽的特征称为字符串插值。

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

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

当您创建$page时,您将使用双引号。一切都很好,直到你到达$mysqli此时,php 尝试将其转换为字符串,以便它可以适应这个$page变量。

您需要像这样用斜杠来转义美元符号。

<?php
$con = new mysqli('localhost', 'mathswi3_data', '...',
'mathswi3_submits');
if ($mysqli->connect_error) {
die('Errr : ('. $mysqli->connect_errno .') '. $mysqli->connect_error);
}

相关内容

最新更新