- functiondb.php туда поместил функции для работы с БД
- db.php класс
- test.php тестирование класса DB
Предсавлю код
functiondb.php
<?php
/**********************************************/
function open()
{
/*Выбрать соответствующую функцию соединения*/
if($this->persistent)
{
$func = 'mysql_pconnect';
}
else
{
$func = 'mysql_connect';
}
/*Соединиться с сервером MySQL*/
$this->conn = $func($this->host,$this->user,$this->password);
if(!$this->conn)
{
return false;
}
/*Выбрать запрошенную БД*/
if(@!mysql_select_db($this->database,$this->conn))
{
return false;
}else{
return true;
}
}
/**********************************************/
function close()
{
return(@mysql_close($this->conn));
}
/**********************************************/
function error()
{
return (mysql_error());
}
/**********************************************/
function query($sql)
{
$this->result = @mysql_query($sql,$this->conn);
return($this->conn != false);
}
/**********************************************/
function affectedRows()
{
return(@mysql_affected_rows($this->conn));
}
/**********************************************/
function numRows()
{
return(@mysql_num_rows($this->result));
}
/**********************************************/
function fetchObject()
{
return(@mysql_fetch_object($this->result,MYSQL_ASSOC));
}
/**********************************************/
function fetchArray()
{
return(@mysql_fetch_array($this->result,MYSQL_NUM));
}
/**********************************************/
function fetchAssoc()
{
return(@mysql_fetch_assoc($this->result));
}
/**********************************************/
function freeResult()
{
return(@mysql_free_result($this->result));
}
?>
db.php
<?php
class DB
{
/*Параметры соединения*/
var $host = '';
var $user = '';
var $password = '';
var $database = '';
var $persistent = false;
/*Дескриптор соединения с базой данных*/
var $conn = NULL;
function DB($host,$user,$password,$database,$persistent = false)
{
$this->host = $host;
$this->user = $user;
$this->password = $password;
$this->database = $database;
$this->persistent = $persistent;
}
}
?>
test.php
<?php
include "db.php";
include "functiondb.php";
$db = new DB("localhost","root","","medical");
if(!$db->open())
{
die($db->error());
}
if(!$db->query("SELECT * FROM test"))
{
die($db->error());
}
while($row = $db->fetchArray())
{
echo "ID - ".$row->id." <br />
TEXT = ".$row->text."";
}
$db->freeResult();
$db->close();
?>
Ошибка выпадает
Fatal error: Call to undefined function: open() in z:\home\light.p\www\test.php on line 7
if(!$db->open())
{
Непонятно.