Return values from Database

0

I'm getting this error:

  

Fatal error: Allowed memory size of 8388608 bytes exhausted (tried to   allocate 1640 bytes) in C: \ AppServ \ www \ muweb \ class \ Connect.php on line   31

When I run these scripts:

index.php

<?php

require_once 'settings/Config.php';
require_once 'class/Connect.php';
require_once 'class/Characters.php';


 $char = new Characters;


?>
<!DOCTYPE html>
<html>
<head>
    <title>SELECT</title>
</head>
<body>
    <table>
        <thead>
            <tr>
                <th>Conta ID</th>
                <th>Personagem</th>
                <th>Dinheiro</th>
            </tr>
        </thead>
        <tbody>
            <?php
            while ($get = $char->fetch()):
            ?>

            <tr>
                <td><?php echo $get['AccountID']; ?></td>
                <td><?php echo $get['Name']; ?></td>
                <td><?php echo $get['Money']; ?></td>
            </tr>
            <?php endwhile; ?>
        </tbody>
    </table>
</body>
</html>

Connect.php

<?php

class Connect {

    private $_cn;
    private $_db;

    public function __construct() {
        $this->Connection();
        $this->Database();
    }

    private function Connection() {
        $this->_cn = mssql_connect(HOST, USER, PASS);

        if ($this->_cn == false) {
            die('Connection error!');
        }
    }

    private function Database() {
        $this->_db = mssql_select_db(BASE, $this->_cn);

        if ($this->_db == false) {
            die('Select database error!');
        }
    }

    public function execute($query) {

        $execute = mssql_query($query);

        if ($execute == false) {
            die('Execute error:' . $query);
        } else {
            return $execute;
        }
    }

    public function row($query) {
        return mssql_fetch_row($query);
    }

    public function fetch($query) {
        return mssql_fetch_array($query);
    }

    public function num($query) {
        return mssql_num_rows($query);
    }
}

characters.php

<?php

class Characters extends Connect {

    public function fetch() {
        $fetch = $this->fetch($this->execute("SELECT * FROM Character WHERE AccountID = 'guialves95'"));
        return $fetch;
    }

    public function num() {
        $num = $this->num($this->execute("SELECT * FROM Character WHERE AccountID = 'guialves95'"));

        return $num;
    }
}

I've done everything but I can not solve the problem, a quick search, some cases have managed to solve by adding this line of code:

ini_set('memory_limit','-1');

It did not work for me, what is the solution to this problem?

I have with me, that, my loop is out of control.

    
asked by anonymous 31.07.2016 / 18:59

1 answer

2

You are extending Class Connect with Characters and rewriting the fetch method. Characters fetch are overriding and so Connect fetch is never used.

What is happening is that your while () is entering an infinite cycle with fetch and only when it occupies all available memory.

Solution : First try renaming your Characters fetch function to another name. The same goes for function num.

[UPDATE]

The fetch_array function must be used within a query. You're using it out.

Here's a solution:

//Connect.php
<?php
public function fetch($query) {
  $results = array();
  while($row = mssql_fetch_array($query))
  {
    $results[] = $row;
  }
  return $results;
}

//Characters.php
public function fetchCharacters() {
  $results = $this->fetch($this->execute("SELECT * FROM Character WHERE AccountID = 'guialves95'"));
}
?>
//Index.php
<?php
foreach ($char->fetchCharacters() as $char):
?>

<tr>
  <td><?php echo $char['AccountID']; ?></td>
  <td><?php echo $char['Name']; ?></td>
  <td><?php echo $char['Money']; ?></td>
</tr>
<?php endforeach; ?>
    
31.07.2016 / 19:23