英文:
Absolute PATH in parse_yaml_file is not working but everything else is perfect
问题
//这个有效
//var_dump(yaml_parse_file("/var/www/omeglall/yaml/connection.yaml");
class MySQL
{
private $HOST;
private $PASSWORD;
private $USER;
private $DATABASE;
private $con;
//路径绝对正确
const CONNECTION_PATH="/var/www/omeglall/yaml/connection.yaml";
static private $connection_YAML;
public function __construct()
{
try
{
$this->load_config();
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$this->con=new mysqli($this->HOST,$this->USER,$this->PASSWORD,$this->DATABASE);
if($this->con->connect_error)
{
throw new Exception("连接失败:". $this->con->connect_error);
}
$this->con->set_charset("utf8mb4");
}catch(Exception $e)
{
$error=$e->getMessage();
echo $error;
}
}
public function getCon()
{
return $this->con;
}
private function load_config()
{
if(empty(self::$connection_YAML)){
//这不起作用!
self::$connection_YAML=yaml_parse_file(self::CONNECTION_PATH);
}
$file=self::$connection_YAML;
$this->HOST=$file["host"];
$this->USER=$file["user"];
$this->PASSWORD=$file["password"];
$this->DATABASE=$file["database"];
}
}
英文:
I'm reading an yaml file inside a class, this file contains information about my database so I can connect to it. Trying to open inside the class doesn't work, trying to open in the global scope works. Relative PATHS doesn't work either. I'm instantiating this class inside another file register-ajax.php, when I instantiate It I get the error: No such file or directory.
Below is the code and the folder structure of my project.
<?php
//This works
//var_dump(yaml_parse_file("/var/www/omeglall/yaml/connection.yaml");
class MySQL
{
private $HOST;
private $PASSWORD;
private $USER;
private $DATABASE;
private $con;
//PATH IS 200% CORRECT
const CONNECTION_PATH="/var/www/omeglall/yaml/connection.yaml";
static private $connection_YAML;
public function __construct()
{
try
{
$this->load_config();
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$this->con=new mysqli($this->HOST,$this->USER,$this->PASSWORD,$this->DATABASE);
if($this->con->connect_error)
{
throw new Exception("Connection failed:". $this->con->connect_error);
}
$this->con->set_charset("utf8mb4");
}catch(Exception $e)
{
$error=$e->getMessage();
echo $error;
}
}
public function getCon()
{
return $this->con;
}
private function load_config()
{
if(empty(self::$connection_YAML)){
//THIS DOESN'T WORK!
self::$connection_YAML=yaml_parse_file(self::CONNECTION_PATH);
}
$file=self::$connection_YAML;
$this->HOST=$file["host"];
$this->USER=$file["user"];
$this->PASSWORD=$file["password"];
$this->DATABASE=$file["database"];
}
}
?>
答案1
得分: -1
错误实际上是由Mysql引起的,而不是函数yaml_parse_file,出于某种原因,Mysql会突然停止运行,然后我不得不重新启动它。
英文:
Turns out the error was coming from Mysql and not from the function yaml_parse_file, for some reason mysql stops running out of nowhere and then I have to start it again
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论