我正在使用Cpanel来开发我的应用程序。它给出了错误
Fatal error: Call to a member function set_charset() on a non-object in /home/restaur /public_html/restaurant/includes/connect_database.php on line 3
当我访问“www.yyyyy.com/filename /”
时这是我的connect_database脚本
<?php
include($_SERVER['DOCUMENT_ROOT'].'/YOUR_FOLDER/variables/variables.php');
$connect->set_charset('utf8');
?>
我的close_database脚本
<?php
include($_SERVER['DOCUMENT_ROOT'].'/YOUR_FOLDER/variables/variables.php');
$connect->close();
?>
我的变量脚本
<?php
// database configuration
$host ="db_host_name";
$user ="db_username";
$pass ="db_pasword";
$database = "database_name";
$connect = new mysqli($host, $user, $pass,$database) or die("Error : ".mysql_error());?>
答案 0 :(得分:3)
它应该像
$host ="db_host_name";
$user ="db_username";
$pass ="db_pasword";
$database = "database_name";
// variables.php
$connect = new mysqli($host, $user, $pass, $database);
/* check connection */
if ($connect->connect_errno) {
printf("Connect failed: %s\n", $connect->connect_error);
exit();
}
现在在connect_database.php
// Make sure your included path is correct
include($_SERVER['DOCUMENT_ROOT'].'/YOUR_FOLDER/variables/variables.php');
global $connect;
$connect->set_charset('utf8');
这将有助于您深入了解global
答案 1 :(得分:0)
我遇到了这个问题,我遇到了编码问题但添加了
$connect->set_charset('utf8');
在之后(我的变量实际上是$conn->set_charset('utf8');
)
$conn = new mysqli($servername, $username, $password, $dbname);
它工作得很漂亮。