I am using a database class in my pages now. However I see to be having problems with executing a mysql_fetch_array.
Here is the database class code:
class Database
{ // Class : begin
var $host; //Hostname, Server
var $password; //Passwort MySQL
var $user; //User MySQL
var $database; //Datenbankname MySQL
var $link;
var $query;
var $result;
var $rows;
function Database()
{ // Method : begin
//Konstruktor
// ********** ADJUST THESE VALUES HERE **********
$this->host = "domain.com";
$this->password = "userpassword";
$this->user = "userID";
$this->database = "database";
$this->rows = 0;
// **********************************************
// **********************************************
//$dbh = mysql_connect($server, $user, $pass, false, 128);
} // Method : end
function OpenLink()
{ // Method : begin
$this->link = @mysql_connect($this->host,$this->user,$this->password) or die (print "Class Database: Error while connecting to DB (link)");
} // Method : end
function SelectDB()
{ // Method : begin
@mysql_select_db($this->database,$this->link) or die (print "Class Database: Error while selecting DB");
} // Method : end
function CloseDB()
{ // Method : begin
mysql_close();
} // Method : end
function Query($query)
{ // Method : begin
$this->OpenLink();
$this->SelectDB();
$this->query = $query;
$this->result = mysql_query($query,$this->link) or die (print "Class Database: Error while executing Query");
// $rows=mysql_affected_rows();
if(ereg("SELECT",$query))
{
$this->dbArray = mysql_fetch_array($this->result,MYSQL_ASSOC);
$this->rows = mysql_num_rows($this->result);
}
$this->CloseDB();
} // Method : end
} // Class : end
What I am trying to do is pull data from database into an array. This will be displayed in table 25 items at a time using pagination. However when I get to the while loop the code slows way down an only displays one set of records before it overloads my webserver and I have to restart apache.
Can someone tell me what I am doing wrong.
Thanks
Here is the pagination code:
include_once('includes/class.database.php');
$database = new Database();
$limit = 25;
$query_count = "SELECT * FROM users";
$database->Query($query_count);
$totalrows = $database->rows;
echo $totalrows;
if(empty($page)){
$page = 1;
}
$limitvalue = $page * $limit - ($limit);
$sql_query = "SELECT * FROM users LIMIT $limitvalue, $limit";
$database->Query($sql_query);
$sql_result = $database->result;
$row = $database->dbArray;
echo $row;
if ($database->rows == 0){
echo("Nothing to Display!");
}
$bgcolor = "#E0E0E0"; // light gray
echo('
<div class="smShadowcontainer">
<div class="innerdiv">
');
//while ($row = mysql_fetch_array($r2, MYSQL_ASSOC))
while($row = $database->dbArray){
if ($bgcolor == "#E0E0E0"){
$bgcolor = "#FFFFFF";
}else{
$bgcolor = "#E0E0E0";
}
echo("<tr bgcolor=".$bgcolor.">n<td>");
echo($row["validation"]);
echo("</td>n<td>");
echo($row["fName"]);
echo("</td>n</tr>");
}
echo("</table>");
if($page != 1){
$pageprev = $page--;
echo("<a href=\"$PHP_SELF&page=$pageprev\">PREV".$limit."</a> ");
}else{
echo("PREV".$limit." ");
}
$numofpages = $totalrows / $limit;
for($i = 1; $i <= $numofpages; $i++){
if($i == $page){
echo($i." ");
}else{
echo("<a href=\"$PHP_SELF?page=$i\">$i</a> ");
}
}
if(($totalrows % $limit) != 0){
if($i == $page){
echo($i." ");
}else{
echo("<a href=\"$PHP_SELF?page=$i\">$i</a> ");
}
}
if(($totalrows - ($limit * $page)) > 0){
$pagenext = $page++;
echo("<a href=\"$PHP_SELF?page=$pagenext\">NEXT".$limit."</a>");
}else{
echo("NEXT".$limit);
}
// mysql_free_result($result);
echo ('
</div>
</div>
');