programing

데이터베이스 오류: 데이터베이스 mydatabase MySQL을 사용할 수 없습니다. 오류: 0() 세션이 중지되었습니다.

iphone6s 2023. 9. 24. 12:32
반응형

데이터베이스 오류: 데이터베이스 mydatabase MySQL을 사용할 수 없습니다. 오류: 0() 세션이 중지되었습니다.

방금 포럼에서 코드를 다운받았는데 mysql에서 코드가 잘 작동하지 않습니다.다음 오류를 보고합니다.

Database error: cannot use database mydatabase MySQL Error: 0 ()Session halted.

이미 구글에서 오류 로그를 찾아서 고치려고 했습니다.

아래 코드는 다음과 같습니다.

<?php

class Database
{
    var $Host     = "127.0.0.1";        // Hostname of our MySQL server.
    var $Database = "mydatabase";               // Logical database name on that server.
    var $User     = "root";                 // User and Password for login.
    var $Password = "mypassword";

    var $Link_ID  = 0;                  // Result of mysqli_connect().
    var $Query_ID = 0;                  // Result of most recent mysqli_query().
    var $Record   = array();            // current mysqli_fetch_array()-result.
    var $Row;                           // current row number.
    var $LoginError = "";

    var $Errno    = 0;                  // error state of query...
    var $Error    = "";

    //-------------------------------------------
    //    Connects to the database
    //-------------------------------------------
    function connect()
    {
        if( 0 == $this->Link_ID )
            $this->Link_ID=mysqli_connect( $this->Host, $this->User, $this->Password );
        if( !$this->Link_ID )
            $this->halt( "Link-ID == false, connect failed" );
        if( !mysqli_query( sprintf( "use %s", $this->Database ), $this->Link_ID ) )
            $this->halt( "cannot use database ".$this->Database );
    }

    //-------------------------------------------
    //    Queries the database
    //-------------------------------------------
    function query( $Query_String )
    {
        $this->connect();
        $this->Query_ID = mysqli_query( $Query_String,$this->Link_ID );
        $this->Row = 0;
        $this->Errno = mysqli_errno();
        $this->Error = mysqli_error();
        if( !$this->Query_ID )
            $this->halt( "Invalid SQL: ".$Query_String );
        return $this->Query_ID;
    }

    //-------------------------------------------
    //    If error, halts the program
    //-------------------------------------------
    function halt( $msg )
    {
        printf( "<strong>Database error:</strong> %s", $msg );
        printf( "<strong>MySQL Error</strong>: %s (%s)", $this->Errno, $this->Error );
        die( "Session halted." );
    }

    //-------------------------------------------
    //    Retrieves the next record in a recordset
    //-------------------------------------------
    function nextRecord()
    {
        @ $this->Record = mysqli_fetch_array( $this->Query_ID );
        $this->Row += 1;
        $this->Errno = mysqli_errno();
        $this->Error = mysqli_error();
        $stat = is_array( $this->Record );
        if( !$stat )
        {
            @ mysqli_free_result( $this->Query_ID );
            $this->Query_ID = 0;
        }
        return $stat;
    }

    //-------------------------------------------
    //    Retrieves a single record
    //-------------------------------------------
    function singleRecord()
    {
        $this->Record = mysqli_fetch_array( $this->Query_ID );
        $stat = is_array( $this->Record );
        return $stat;
    }

    //-------------------------------------------
    //    Returns the number of rows  in a recordset
    //-------------------------------------------
    function numRows()
    {
        return mysqli_num_rows( $this->Query_ID );
    }

    //-------------------------------------------
    //    Returns the Last Insert Id
    //-------------------------------------------
    function lastId()
    {
        return mysqli_insert_id();
    }

    //-------------------------------------------
    //    Returns Escaped string
    //-------------------------------------------
    function mysqli_escape_mimic($inp)
    {
        if(is_array($inp))
            return array_map(__METHOD__, $inp);
        if(!empty($inp) && is_string($inp))
        {
            return str_replace(array('\\', "\0", "\n", "\r", "'", '"', "\x1a"), array('\\\\', '\\0', '\\n', '\\r', "\\'", '\\"', '\\Z'), $inp);
        }
        return $inp;
    }
    //-------------------------------------------
    //    Returns the number of rows  in a recordset
    //-------------------------------------------
    function affectedRows()
    {
        return mysqli_affected_rows();
    }

    //-------------------------------------------
    //    Returns the number of fields in a recordset
    //-------------------------------------------
    function numFields()
    {
        return mysqli_num_fields($this->Query_ID);
    }

}

?>

Stack Overflow에서 제안한 다른 방법을 이미 시도해 보았습니다.

PHP 설명서에서

절차양식

mysqli_query ( mysqli $link , string $query [, int $resultmode = MYSQLI_STORE_RESULT ] ) : mixed

매개변수

link: 에서 반환된 링크 식별자mysqli_connect()문의하다

쿼리: 쿼리 문자열입니다.

첫 번째 매개 변수로 sql 문을 전달한 다음 연결 핸들을 전달하므로 코드가 작동할 수 없습니다.데이터베이스를 지정할 수도 있습니다.mysqli_connect4번째 파라미터로.클라이언트 서버 핸드셰이크 중에 기본 데이터베이스가 설정되므로 이 작업이 더 빠릅니다.

언급URL : https://stackoverflow.com/questions/58798385/database-error-cannot-use-database-mydatabase-mysql-error-0-session-halted

반응형