如何在php fwrite中有一个mysqli语句

时间:2015-03-09 19:36:12

标签: php mysqli 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,因为它“无法转换成字符串......”

1 个答案:

答案 0 :(得分:1)

您在这里被忽视的功能称为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);
}