`parse_yaml_file` 中的绝对路径不起作用,但其他一切都完美。

huangapple go评论73阅读模式
英文:

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"];

    }

}

?>

Folder Structures

答案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

huangapple
  • 本文由 发表于 2023年2月27日 19:05:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/75579645.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定