Return values from Database

Asked

Viewed 44 times

0

I’m getting this mistake:

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);
    }
}

php characters.

<?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 can’t solve the problem, a quick search, some cases managed to solve by adding this line of code:

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

With me it did not work, what is the solution to this problem?

I got with me, that, man loop is out of control.

  • You are calling the function fetch all the time, change the $this for parent, was to call the Connect class right?

  • @fernandoandrade is not necessary. With more time I explain why.

  • My mistake is necessary yes, sorry.

1 answer

2


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

What is happening is that your while() is going into infinite cycle with fetch and only stops when it takes up all available memory.

Solution: First try to rename your fetch function from Characters to another name. The same happens with the function in one.

[UPDATE]

The fetch_array function should be used within a query. You are using it outside.

Here is 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; ?>
  • I will review these concepts and come back with news.

  • etnos, I changed the function name fetch() he returned me the values, but not all I need, I have 3 Characters in my DB, and he returned me only 1 with infinite loop yet. See image http://i.imgur.com/m9zWd6g.png

  • @Guilhermespinxo updated the answer. See if this works.

  • didn’t work,... :/

  • Warning: Invalid argument supplied for foreach() EDIT: It worked, had to return $Results

Browser other questions tagged

You are not signed in. Login or sign up in order to post.