我在名为config.php的文件中有这个mysqli_connect:
$DbServer = 'localhost';
$DbUser = 'username';
$DbPassword = 'password';
$DbName = 'dbname';
$con = mysqli_connect($DbServer, $DbUser, $DbPassword, $DbName);
然后我有另一个名为sendingrequest.php的PHP文件:
<?php
session_start();
require_once ('config.php');
$Key = mysqli_real_escape_string ($con, $_GET["key"]);
...
?>
此sendingrequest.php始终产生此错误消息:
Warning: mysqli_real_escape_string(): Couldn't fetch mysqli in /home/public_html/sendingrequest.php on line 4
我尝试通过将mysqli_connect()放在同一个文件上而不是通过require_once()来修改脚本,并且令人惊讶的是它有效......
<?php
session_start();
//require_once ('config.php');
$DbServer = 'localhost';
$DbUser = 'username';
$DbPassword = 'password';
$DbName = 'dbname';
$con = mysqli_connect($DbServer, $DbUser, $DbPassword, $DbName);
$Key = mysqli_real_escape_string ($con, $_GET["key"]);
...
?>
我的require_once出了什么问题?为什么mysqli_real_escape_string无法使用它?谢谢!
答案 0 :(得分:-2)
我不知道为什么当两个命令放在同一个文件中时它起作用,但你应该使用这种面向对象的实例化方法。
$con = new mysqli($DbServer, $DbUser, $DbPassword, $DbName);
$Key = $con->mysqli_real_escape_string ($_GET["key"]);