Ben problem sınıfını kullanmaya çalıştığınızda aşağıdaki hatayı alıyorum:
Fatal error: Class 'database' not found in path/problem.php on line 25
Ben database.php gerektiren problem.php üstündeki, bu hata var neden anlamıyorum. Ne oluyor?
problem.php
<?php
require("common.php");
require("database.php");
class problem
{
// variable declaration
protected $question;
protected $answer;
protected $tags;
protected $problem_id;
protected $commonStuff;
protected $userID;
protected $db;
// parameters for $var = new problem(question, problem)
function __construct($question_in="", $problem_in="") {
$this->question = $question_in;
$this->answer = $answer_in;
$this->tags = array();
$this->commonStuff = new common();
$this->userID = $this->commonStuff->getUserID();
$this->db = new database();
}
// set the question and answer
function set_question($new_question) {
$this->question = $new_question;
}
function set_answer($new_question) {
$this->answer = $new_answer;
}
// get the question and answer
function get_question() {
return $this->question;
}
function get_answer() {
return $this->answer;
}
function get_pid() {
return $this->problem_id;
}
// add tag
function add_tag($new_tag) {
$this->tags[] = $new_tag;
$this->tags = array_unique($this->tags);
}
// check for errors
function error_check_question() {
if (empty($this->question))
return true;
else
return false;
}
function error_check_answer() {
if (empty($this->answer))
return true;
else
return false;
}
// get ready to insert into database
function code_parse($var) {
// due math code processing
preg_match_all("/(?<=<math>)(.*?)(?=<\/math>)/", $var,$questionMath);
foreach($questionMath[0] as $math) {
$imgElement = "<img src='http://chart.apis.google.com/chart?cht=tx&chl=" . $commonStuff->mathToLatex($math) . "' />";
$question = str_replace("<math>" . $math . "</math>", $imgElement, $question);
}
$var = safe($var);
return $var;
}
function safe($str) {
return mysql_real_escape_string($str);
}
function build_insert_SQL() {
$sql = "INSERT INTO problems (userid, question, answer, date, nextTimeToAnswer, correctStreak) VALUES(" . $this->userID . ", '" . code_parse($this->question) . "', '" . code_parse($this->answer) . "', '" . $this->date . "', '" . $this->date . "', 0)";
$this->problem_id = mysql_insert_id();
return $sql;
}
// build the sql for tags
function insert_tag_SQL() {
foreach($this->tags as $tag_val) {
if (empty($tag_val) == false) {
// check if the tag exists
$result = $db->select_row("SELECT id FROM tags WHERE name = '$tag_val'");
if (empty($result)) {
$db->execute("INSERT INTO tags (name) VALUES('$tag_val')");
$tagID = mysql_insert_id();
}
else {
$tagID = $result['id'];
}
// insert into tagmap
$result = $db->select_row("SELECT tag_id FROM tagmap WHERE
= " . $this->problem_id . " AND tag_id = $tagID");
if (empty($result) == false) {
$sql = "INSERT INTO tagmap (problem_id,tag_id) VALUES($problem_id, $tagID)";
$db->execute($sql);
}
}
}
}
// insert the problem into the database
function insert() {
$sql = build_insert_SQL();
$db->execute($sql);
insert_tag_SQL();
}
}
?>
database.php
<?php
class database
{
// variable declaration
protected $host;
protected $databaseUser;
protected $databasePassword;
protected $databaseName;
protected $con;
function __construct() {
$this->host = "localhost";
$this->databaseUser = "sambend1_sam";
$this->databasePassword = "samjosh";
$this->databaseName = "sambend1_brandon";
$this->con = mysql_connect($host,$databaseUser,$databasePassword);
return;
}
// select rows from database
function select($query) {
$result = mysql_query($query) or die ('This was an error: '.mysql_error ());
return $result;
}
// select an individual row and put it into a var
function select_row($query) {
$result = mysql_query($query) or die ('This was an error: '.mysql_error ());
while($row = mysql_fetch_array($result)) {
$toReturn = $row;
}
return $toReturn;
}
// execute a query, such as insert or update
function execute($query) {
mysql_query($query) or die ('This was an error: '.mysql_error ());
}
}
?>