Tenho uma classe que é responsavel pela conexão e consulta de dados no MYSQL.
Estou querendo coloca-la em um arquivo separado, e apenas chamar os métodos na página principal....
Porém apresenta o seguinte erro:
Fatal error: Class 'Database' not found in C:\wamp\www\ad\HTML\index3.php on line 7
Quando coloca a classe na mesma página funciona tudo perfeitamente. O que será que estou fazendo de errado....
database.php
<?
class Database
{
var $Host = "localhost"; // Hostname of our MySQL server.
var $Database = "alcooldrogas"; // Logical database name on that server.
var $User = "root"; // User and Password for login.
var $Password = "";
var $Link_ID = 0; // Result of mysql_connect().
var $Query_ID = 0; // Result of most recent mysql_query().
var $Record = array(); // current mysql_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=mysql_connect( $this->Host, $this->User, $this->Password );
if( !$this->Link_ID )
$this->halt( "Link-ID == false, connect failed" );
if( !mysql_query( sprintf( "use %s", $this->Database ), $this->Link_ID ) )
$this->halt( "cannot use database ".$this->Database );
} // end function connect
//-------------------------------------------
// Queries the database
//-------------------------------------------
function query( $Query_String )
{
$this->connect();
$this->Query_ID = mysql_query( $Query_String,$this->Link_ID );
$this->Row = 0;
$this->Errno = mysql_errno();
$this->Error = mysql_error();
if( !$this->Query_ID )
$this->halt( "Invalid SQL: ".$Query_String );
return $this->Query_ID;
} // end function query
//-------------------------------------------
// If error, halts the program
//-------------------------------------------
function halt( $msg )
{
printf( "</td></tr></table><b>Database error:</b> %s<br>n", $msg );
printf( "<b>MySQL Error</b>: %s (%s)<br>n", $this->Errno, $this->Error );
die( "Session halted." );
} // end function halt
//-------------------------------------------
// Retrieves the next record in a recordset
//-------------------------------------------
function nextRecord()
{
@ $this->Record = mysql_fetch_array( $this->Query_ID );
$this->Row += 1;
$this->Errno = mysql_errno();
$this->Error = mysql_error();
$stat = is_array( $this->Record );
if( !$stat )
{
@ mysql_free_result( $this->Query_ID );
$this->Query_ID = 0;
}
return $stat;
} // end function nextRecord
//-------------------------------------------
// Retrieves a single record
//-------------------------------------------
function singleRecord()
{
$this->Record = mysql_fetch_array( $this->Query_ID );
$stat = is_array( $this->Record );
return $stat;
} // end function singleRecord
//-------------------------------------------
// Returns the number of rows in a recordset
//-------------------------------------------
function numRows()
{
return mysql_num_rows( $this->Query_ID );
} // end function numRows
} // end class Database
?>
index.php
<?php
include 'database.php';
// create an instance of the Database class and call it $db
$db = new Database;
// do a query to retrieve a single record
$Query = "SELECT * FROM cadastroad1";
$db->query($Query); // query the database
$db->singleRecord(); // retrieve a single record
echo "cid> ".$db->Record['CID']; // output a field value from the recordset
// do a query to retrieve multiple records
$Query = "SELECT * FROM cadastroad1";
$db->query($Query); // query the database
while ($db->nextRecord())
{
echo $db->Record['Sexo']."<br>"; // output a field value from the recordset
} // end while loop going through whole recordset
?>
já tentei usar o include dessa forma tb: include("database.php"); mas nada tb....
Pergunta
MalloryKnox
Olá, pessoal !
Estou com o seguinte problema:
Tenho uma classe que é responsavel pela conexão e consulta de dados no MYSQL.
Estou querendo coloca-la em um arquivo separado, e apenas chamar os métodos na página principal....
Porém apresenta o seguinte erro:
Fatal error: Class 'Database' not found in C:\wamp\www\ad\HTML\index3.php on line 7
Quando coloca a classe na mesma página funciona tudo perfeitamente. O que será que estou fazendo de errado....
database.php <? class Database { var $Host = "localhost"; // Hostname of our MySQL server. var $Database = "alcooldrogas"; // Logical database name on that server. var $User = "root"; // User and Password for login. var $Password = ""; var $Link_ID = 0; // Result of mysql_connect(). var $Query_ID = 0; // Result of most recent mysql_query(). var $Record = array(); // current mysql_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=mysql_connect( $this->Host, $this->User, $this->Password ); if( !$this->Link_ID ) $this->halt( "Link-ID == false, connect failed" ); if( !mysql_query( sprintf( "use %s", $this->Database ), $this->Link_ID ) ) $this->halt( "cannot use database ".$this->Database ); } // end function connect //------------------------------------------- // Queries the database //------------------------------------------- function query( $Query_String ) { $this->connect(); $this->Query_ID = mysql_query( $Query_String,$this->Link_ID ); $this->Row = 0; $this->Errno = mysql_errno(); $this->Error = mysql_error(); if( !$this->Query_ID ) $this->halt( "Invalid SQL: ".$Query_String ); return $this->Query_ID; } // end function query //------------------------------------------- // If error, halts the program //------------------------------------------- function halt( $msg ) { printf( "</td></tr></table><b>Database error:</b> %s<br>n", $msg ); printf( "<b>MySQL Error</b>: %s (%s)<br>n", $this->Errno, $this->Error ); die( "Session halted." ); } // end function halt //------------------------------------------- // Retrieves the next record in a recordset //------------------------------------------- function nextRecord() { @ $this->Record = mysql_fetch_array( $this->Query_ID ); $this->Row += 1; $this->Errno = mysql_errno(); $this->Error = mysql_error(); $stat = is_array( $this->Record ); if( !$stat ) { @ mysql_free_result( $this->Query_ID ); $this->Query_ID = 0; } return $stat; } // end function nextRecord //------------------------------------------- // Retrieves a single record //------------------------------------------- function singleRecord() { $this->Record = mysql_fetch_array( $this->Query_ID ); $stat = is_array( $this->Record ); return $stat; } // end function singleRecord //------------------------------------------- // Returns the number of rows in a recordset //------------------------------------------- function numRows() { return mysql_num_rows( $this->Query_ID ); } // end function numRows } // end class Database ?>index.php<?php include 'database.php'; // create an instance of the Database class and call it $db $db = new Database; // do a query to retrieve a single record $Query = "SELECT * FROM cadastroad1"; $db->query($Query); // query the database $db->singleRecord(); // retrieve a single record echo "cid> ".$db->Record['CID']; // output a field value from the recordset // do a query to retrieve multiple records $Query = "SELECT * FROM cadastroad1"; $db->query($Query); // query the database while ($db->nextRecord()) { echo $db->Record['Sexo']."<br>"; // output a field value from the recordset } // end while loop going through whole recordset ?>já tentei usar o include dessa forma tb: include("database.php"); mas nada tb....
Link para o comentário
Compartilhar em outros sites
1 resposta a esta questão
Posts Recomendados
Participe da discussão
Você pode postar agora e se registrar depois. Se você já tem uma conta, acesse agora para postar com sua conta.