Re: Simple MySQL sample code runs out of memory

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Also, I don't know how your displaying this info,
but if you have a good number of entries,
you may want to look into pagination.
Then your only grabbing a certain number of sets at a time not the whole database.

Best,
Karl

On Oct 28, 2011, at 12:29 AM, Karl DeSaulniers wrote:

Hmmm.. Try this..

 for($i=0; $i<$num_rows; $i++){
    $c = mysql_result($result,$i,"contract");

    echo sprintf( "|%13d |%7d |\n", $c, $i );
	mysql_free_result($c); //Free the memory with this.
 }

or this..

 for($i=0; $i<$num_rows; $i++){
    $c = mysql_result($result,$i,"contract");

    echo sprintf( "|%13d |%7d |\n", $c, $i );
	mysql_free_result($result); //Free the memory with this.
 }

I haven't used it in a long time, so not positive if you free the $result or what you grab from $result ($c) Also, it might be mysql_freeresult(), if mysql_free_result() doesn't work, try that.

There is also the off chance that I am mixing things. You may need to use mysql_fetch_assoc() to use the free results function.
something like..

while ($row = mysql_fetch_assoc($result)) {
   echo $row["contract"];
}

mysql_free_result($result);


But I think it will fix your memory error.
LMK,

HTH,
Best,

Karl



On Oct 27, 2011, at 11:57 PM, php@xxxxxxxxxxxxx wrote:

On Thu, Oct 27, 2011 at 10:43:48PM -0500, Karl DeSaulniers wrote:
Maybe this..
HTH,

<?php
...

$result = mysql_query( $qry, $db_conn ) or die( mysql_error() . "\n" );

$num_rows = mysql_numrows($result);
if(!$result || ($num_rows <= 0)){
  echo "Error displaying info";
}
else if($num_rows > 0){
  for($i=0; $i<$num_rows; $i++){
     $c = mysql_result($result,$i,"contract");

     echo sprintf( "|%13d |%7d |\n", $c, $i );
  }
}

?>

Best,
Karl

Wow, working code!  Thanks.  I did try it:

<?php

require_once( '../include/mysql_funcs.php' );

$test_db_host = "localhost";
$test_db_user = "msm";
$test_db_pwd  = "EetGiOj6";
$test_db_name = "test_msm";

$db_host = $test_db_host;
$db_user = $test_db_user;
$db_name = $test_db_name;
$db_pwd  = $test_db_pwd;

if (!($db_conn = mysql_connect( $db_host, $db_user, $db_pwd )))
	die( "Can't connect to MySQL server\n" );

if (!mysql_select_db( $db_name, $db_conn ))
	die( "Can't connect to database $db_name\n" );

$qry = "select * from test_spot_table order by contract_seq";

$result = mysql_query( $qry, $db_conn ) or die( mysql_error() . "\n" );

$num_rows = mysql_numrows($result);
if(!$result || ($num_rows <= 0)){
 echo "Error displaying info";
}
else if($num_rows > 0){
 for($i=0; $i<$num_rows; $i++){
    $c = mysql_result($result,$i,"contract");

    echo sprintf( "|%13d |%7d |\n", $c, $i );
 }
}

?>

But it does the same thing:

PHP Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 20 bytes) in /usr/local/www/apache22/data/ bogus_copy/xx4.php on line 31

Line 31 is:

  31        $c = mysql_result($result,$i,"contract");


Karl DeSaulniers
Design Drumm
http://designdrumm.com


--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


Karl DeSaulniers
Design Drumm
http://designdrumm.com


--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux