dr637349 2012-02-16 15:56
浏览 12
已采纳

获取实际打开的db连接

How can I check if a db-connection is open in PHP?

How can I get a reference to this connection?

How can I determine if it is MySQL, Postgres or something else?

  • 写回答

4条回答 默认 最新

  • drqwbh2150 2012-02-16 16:02
    关注

    Ideally you would know the variable the connection is saved into. Without this it becomes a hassle to find it. It means you would have to loop through every variable and test each one, but doing this for any reason can be a pain because it wont find variables outside of the current scope. You can loop the current variables using:

    foreach(get_defined_vars() as $x){
        // Code
    }
    

    To check if a variable is a valid connection you can use the below code, it should return an integer:

    if(mysql_thread_id($connection){
        // Connection exists
    }
    

    Once you know its a connection you can use the following to return the type of connection, eg, mysql.

    echo get_resource_type($connection);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?