在第9行的/home1/ksuexpre/public_html/conn.php中调用boolean上的成员函数set_charset()

时间:2017-05-11 09:30:05

标签: php sql mysqli

我的项目运作良好,但突然显示错误显示:

  

“在布尔值中调用成员函数set_charset()   /home1/express/public_html/conn.php在第9行“

这是我的conn.php

<?php
$db_name = "";
$server_name = "";
$username = "";
$password = "";

// Create connection
$conn = mysqli_connect($server_name, $username, $password, $db_name);
$conn->set_charset("utf8"); `


if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

?>

1 个答案:

答案 0 :(得分:1)

在设置字符集

之前,您需要检查是否有mysqli对象

更改此行

$conn = mysqli_connect($server_name, $username, $password, $db_name);
$conn->set_charset("utf8"); `

 if (mysqli_connect_errno()) {  
     printf("Connect failed: %s\n", mysqli_connect_error()); 
     exit();
 }

$conn = mysqli_connect($server_name, $username, $password, $db_name);


 if (mysqli_connect_errno()) {  
     printf("Connect failed: %s\n", mysqli_connect_error()); 
     exit();
 }
$conn->set_charset("utf8"); `