php-general Digest 30 Sep 2009 22:38:03 -0000 Issue 6366
Topics (messages 298411 through 298420):
Re: Paging script
298411 by: Mert Oztekin
298412 by: Angelo Zanetti
298413 by: Ashley Sheridan
298414 by: Ruben Crespo
298415 by: Angelo Zanetti
298416 by: Mert Oztekin
298417 by: Ruben Crespo
Re: Where's my memory going?!
298418 by: Philip Thompson
298419 by: Philip Thompson
298420 by: Ralph Deffke
Administrivia:
To subscribe to the digest, e-mail:
[email protected]
To unsubscribe from the digest, e-mail:
[email protected]
To post to the list, e-mail:
[email protected]
----------------------------------------------------------------------
--- Begin Message ---
Sorry but I dont understand what you mean. Can you be more specific?
{1} 2 3 ... 12 (Startup / Total 12 pages)
When you click 7
1 ... 6 {7} 8 ... 12
When you click 11 or 12
1 ... 10 11 {12}
Do you mean something like this?
-----Original Message-----
From: Angelo Zanetti [mailto:[email protected]]
Sent: Wednesday, September 30, 2009 1:21 PM
To: [email protected]
Subject: [PHP] Paging script
Hi All,
I am looking for a paging script, not the normal type but slightly
different. It must show as follows:
1 2 3 ... 12
Then:
1 ... 678 ... 12
And
1 ... 10 11 12
I have googled and not really found much and don't really want to reinvent
the wheel. I am sure I can write my own but due to time constraints ill like
to get a script that will assist me.
Thanks in advance
http://www.wapit.co.za
http://www.elemental.co.za
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Bu mesaj ve ekleri, mesajda g?nderildi?i belirtilen ki?i/ki?ilere ?zeldir ve
gizlidir. Size yanl??l?kla ula?m??sa l?tfen g?nderen kisiyi bilgilendiriniz ve
mesaj? sisteminizden siliniz. Mesaj ve eklerinin i?eri?i ile ilgili olarak
?irketimizin herhangi bir hukuki sorumlulu?u bulunmamaktad?r. ?irketimiz
mesaj?n ve bilgilerinin size de?i?ikli?e u?rayarak veya ge? ula?mas?ndan,
b?t?nl???n?n ve gizlili?inin korunamamas?ndan, vir?s i?ermesinden ve bilgisayar
sisteminize verebilece?i herhangi bir zarardan sorumlu tutulamaz.
This message and attachments are confidential and intended for the
individual(s) stated in this message. If you received this message in error,
please immediately notify the sender and delete it from your system. Our
company has no legal responsibility for the contents of the message and its
attachments. Our company shall have no liability for any changes or late
receiving, loss of integrity and confidentiality, viruses and any damages
caused in anyway to your computer system.
--- End Message ---
--- Begin Message ---
-----Original Message-----
From: Mert Oztekin [mailto:[email protected]]
Sent: 30 September 2009 12:40 PM
To: 'Angelo Zanetti'; [email protected]
Subject: RE: [PHP] Paging script
Sorry but I dont understand what you mean. Can you be more specific?
{1} 2 3 ... 12 (Startup / Total 12 pages)
When you click 7
1 ... 6 {7} 8 ... 12
When you click 11 or 12
1 ... 10 11 {12}
Do you mean something like this?
Hi Mert,
Yes I mean exactly like you have it there,
Regards
Angelo
--- End Message ---
--- Begin Message ---
On Wed, 2009-09-30 at 12:59 +0200, Angelo Zanetti wrote:
>
> -----Original Message-----
> From: Mert Oztekin [mailto:[email protected]]
> Sent: 30 September 2009 12:40 PM
> To: 'Angelo Zanetti'; [email protected]
> Subject: RE: [PHP] Paging script
>
> Sorry but I dont understand what you mean. Can you be more specific?
>
> {1} 2 3 ... 12 (Startup / Total 12 pages)
> When you click 7
> 1 ... 6 {7} 8 ... 12
> When you click 11 or 12
> 1 ... 10 11 {12}
>
> Do you mean something like this?
>
>
> Hi Mert,
>
> Yes I mean exactly like you have it there,
>
> Regards
> Angelo
>
>
>
I'd just code it yourself, the logic behind it is pretty simple.
* The numbers 1 and 12 (the total) will always display
* The page you are on will display the number to either side of
it, unless that is 1 or 12 (the total)
* If the page you are on is 2 or 11 (one less than the total) then
add in '...' on either side of the middle block.
Thanks,
Ash
http://www.ashleysheridan.co.uk
--- End Message ---
--- Begin Message ---
Hi all,
Maybe it is not just what you need but could be helpfull.
Here you have the code:
//-------------------paginator.php----------------------------------------------------------------------------
<?php
function paginator ($current, $total, $per_page, $link, $limit) {
if ($current == '')
{
$current = 1;
}else{
$current = $current;
}
$total_pages = ceil($total/$per_page);
$previous = $current - 1;
$next = $current + 1;
/*traduction*/
$toFrom = "<p>Page ".$current." of ".$total_pages."</p>";
$Goto = "Go to Page";
$go = "Go";
$texto = "
<div id='paginacion'>".$toFrom."
<ul>";
if ($current > 1)
$texto .= "<li><a href=\"$link$previous\">«</a></li>";
else
$texto .= "<li><b>«</b></li>";
//Se lista hasta llegar a la posición actual
if ($current > $limit) {
$start = ($current - $limit) + 1;
$texto .= "<li><b>...</b></li>";
} else {
$start = 1;
}
for ($start; $start < $current; $start++) {
$texto .= "<li><a href=\"$link$start\">$start</a></li>";
}
//mostramos posicion actual
$texto .= "<li><b>$current</b></li> ";
//mostramos resto de registros
for ($i=$current+1; $i<=$total_pages; $i++) {
if ($i > $limit)
{
$texto .= "<li><b>...</b></li>";
$i = $total_pages;
}else{
$texto .= "<li><a href=\"$link$i\">$i</a></li> ";
}
}
if ($current < $total_pages)
{
$texto .= "<li><a href=\"$link$next\">»</a></li>";
}else{
$texto .= "<li><b>»</b></li>";
}
$texto .= "</ul>";
//a form to go directly to the page you want
$texto .= '<!--FORMULARIO PAGINADOR-->
<div id="form_ir">
<form action="'.$_SERVER["PHP_SELF"].'" method="get">
<fieldset>
<p>'.$Goto.' <input type="text" size="3" name="page" value="" />
<input type="submit" name="ir" value="'.$go.'" /></p>
</fieldset>
</form>
</div>';
$texto .= '<br class="break" />
</div>
';
return $texto;
}
?>
//-------------------paginator.php----------------------------------------------------------------------------
//-------------------example.php----------------------------------------------------------------------------
<?php
include ("paginator.php");
for ($n=0; $n<=100; $n++)
{
$myArray [$n] = $n;
}
$total = count ($myArray); //the total elements in the array
$per_page = 5; //elements the script show
$link = "example.php?page="; //link
$limit = 3; //number of items you show in the paginator list
if ($_GET["page"])
{
$current = $_GET["page"];
}else{
$current = $_POST["page"];
}
$start = ($current-1) * $per_page;
$end = $start + $per_page;
//call to the paginator function ...
echo paginator ($current, $total, $per_page, $link, $limit);
//Show the results in different pages
for ($i = $start; $i<=$end ;$i++)
{
echo $myArray[$i]."<br />";
}
?>
//-------------------example.php----------------------------------------------------------------------------
2009/9/30 Angelo Zanetti <[email protected]>
>
> Hi All,
>
> I am looking for a paging script, not the normal type but slightly
> different. It must show as follows:
>
> 1 2 3 ... 12
>
> Then:
>
> 1 ... 678 ... 12
>
> And
>
> 1 ... 10 11 12
>
> I have googled and not really found much and don't really want to reinvent
> the wheel. I am sure I can write my own but due to time constraints ill
> like
> to get a script that will assist me.
>
> Thanks in advance
> http://www.wapit.co.za
> http://www.elemental.co.za
>
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
--
http://peachep.wordpress.com
--- End Message ---
--- Begin Message ---
_____
From: Ruben Crespo [mailto:[email protected]]
Sent: 30 September 2009 01:23 PM
To: Angelo Zanetti
Cc: [email protected]
Subject: Re: [PHP] Paging script
Hi all,
Maybe it is not just what you need but could be helpfull.
Here you have the code:
//-------------------paginator.php------------------------------------------
----------------------------------
<?php
function paginator ($current, $total, $per_page, $link, $limit) {
if ($current == '')
{
$current = 1;
}else{
$current = $current;
}
$total_pages = ceil($total/$per_page);
$previous = $current - 1;
$next = $current + 1;
/*traduction*/
$toFrom = "<p>Page ".$current." of ".$total_pages."</p>";
$Goto = "Go to Page";
$go = "Go";
$texto = "
<div id='paginacion'>".$toFrom."
<ul>";
if ($current > 1)
$texto .= "<li><a href=\"$link$previous\">«</a></li>";
else
$texto .= "<li><b>«</b></li>";
//Se lista hasta llegar a la posición actual
if ($current > $limit) {
$start = ($current - $limit) + 1;
$texto .= "<li><b>...</b></li>";
} else {
$start = 1;
}
for ($start; $start < $current; $start++) {
$texto .= "<li><a href=\"$link$start\">$start</a></li>";
}
//mostramos posicion actual
$texto .= "<li><b>$current</b></li> ";
//mostramos resto de registros
for ($i=$current+1; $i<=$total_pages; $i++) {
if ($i > $limit)
{
$texto .= "<li><b>...</b></li>";
$i = $total_pages;
}else{
$texto .= "<li><a href=\"$link$i\">$i</a></li> ";
}
}
if ($current < $total_pages)
{
$texto .= "<li><a href=\"$link$next\">»</a></li>";
}else{
$texto .= "<li><b>»</b></li>";
}
$texto .= "</ul>";
//a form to go directly to the page you want
$texto .= '<!--FORMULARIO PAGINADOR-->
<div id="form_ir">
<form action="'.$_SERVER["PHP_SELF"].'" method="get">
<fieldset>
<p>'.$Goto.' <input type="text" size="3" name="page" value="" />
<input type="submit" name="ir" value="'.$go.'" /></p>
</fieldset>
</form>
</div>';
$texto .= '<br class="break" />
</div>
';
return $texto;
}
?>
//-------------------paginator.php------------------------------------------
----------------------------------
//-------------------example.php--------------------------------------------
--------------------------------
<?php
include ("paginator.php");
for ($n=0; $n<=100; $n++)
{
$myArray [$n] = $n;
}
$total = count ($myArray); //the total elements in the array
$per_page = 5; //elements the script show
$link = "example.php?page="; //link
$limit = 3; //number of items you show in the paginator list
if ($_GET["page"])
{
$current = $_GET["page"];
}else{
$current = $_POST["page"];
}
$start = ($current-1) * $per_page;
$end = $start + $per_page;
//call to the paginator function ...
echo paginator ($current, $total, $per_page, $link, $limit);
//Show the results in different pages
for ($i = $start; $i<=$end ;$i++)
{
echo $myArray[$i]."<br />";
}
?>
//-------------------example.php--------------------------------------------
--------------------------------
Thanks ruben very much :-)
Regards
Angelo
http://www.wapit.co.za
http://www.elemental.co.za
--- End Message ---
--- Begin Message ---
This should do it. i didnt have time to test so there may be some bugs.
function pageit($limit , $total, $page, $link)
{
$last = intval($total / $limit) + ($total % $limit ==0 ? 0:1);
if(!is_numeric($page) || $page > $last)
$page = 1;
$pages[] = 1;
if($page == 1)
{
$pages[] = 2;
$pages[] = 3;
}
else
if($page == $last)
{
$pages[]= $last - 2;
$pages[] = $last -1;
}
else
{
if($page - 1 > 1)
$pages[] = $page -1;
$pages[] = $page;
if($page +1 < $last)
$pages[] = $page +1;
}
$pages[] = $last;
for($i=0; $i < count($pages) ; $i++)
{
if($pages[$i] <1 || $pages[$i] > $last)
continue;
if($i>0)
if($pages[$i] - $pages[$i-1] > 1 )
echo '...';
echo '<a href="' . $link . '?page=' . $pages[$i]. '">' .
$pages[$i] . '</a>';
}
}
-----Original Message-----
From: Angelo Zanetti [mailto:[email protected]]
Sent: Wednesday, September 30, 2009 1:59 PM
To: Mert Oztekin; [email protected]
Subject: RE: [PHP] Paging script
-----Original Message-----
From: Mert Oztekin [mailto:[email protected]]
Sent: 30 September 2009 12:40 PM
To: 'Angelo Zanetti'; [email protected]
Subject: RE: [PHP] Paging script
Sorry but I dont understand what you mean. Can you be more specific?
{1} 2 3 ... 12 (Startup / Total 12 pages)
When you click 7
1 ... 6 {7} 8 ... 12
When you click 11 or 12
1 ... 10 11 {12}
Do you mean something like this?
Hi Mert,
Yes I mean exactly like you have it there,
Regards
Angelo
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Bu mesaj ve ekleri, mesajda g?nderildi?i belirtilen ki?i/ki?ilere ?zeldir ve
gizlidir. Size yanl??l?kla ula?m??sa l?tfen g?nderen kisiyi bilgilendiriniz ve
mesaj? sisteminizden siliniz. Mesaj ve eklerinin i?eri?i ile ilgili olarak
?irketimizin herhangi bir hukuki sorumlulu?u bulunmamaktad?r. ?irketimiz
mesaj?n ve bilgilerinin size de?i?ikli?e u?rayarak veya ge? ula?mas?ndan,
b?t?nl???n?n ve gizlili?inin korunamamas?ndan, vir?s i?ermesinden ve bilgisayar
sisteminize verebilece?i herhangi bir zarardan sorumlu tutulamaz.
This message and attachments are confidential and intended for the
individual(s) stated in this message. If you received this message in error,
please immediately notify the sender and delete it from your system. Our
company has no legal responsibility for the contents of the message and its
attachments. Our company shall have no liability for any changes or late
receiving, loss of integrity and confidentiality, viruses and any damages
caused in anyway to your computer system.
--- End Message ---
--- Begin Message ---
O.K Angelo,
I wish you that you find the code useful for your projects.
Best Regards !!
Rubén Crespo
2009/9/30 Angelo Zanetti <[email protected]>
>
>
>
>
> _____
>
> From: Ruben Crespo [mailto:[email protected]]
> Sent: 30 September 2009 01:23 PM
> To: Angelo Zanetti
> Cc: [email protected]
> Subject: Re: [PHP] Paging script
>
>
>
> Hi all,
>
> Maybe it is not just what you need but could be helpfull.
>
> Here you have the code:
>
>
> //-------------------paginator.php------------------------------------------
> ----------------------------------
> <?php
> function paginator ($current, $total, $per_page, $link, $limit) {
>
> if ($current == '')
> {
> $current = 1;
> }else{
> $current = $current;
> }
>
> $total_pages = ceil($total/$per_page);
> $previous = $current - 1;
> $next = $current + 1;
>
>
> /*traduction*/
> $toFrom = "<p>Page ".$current." of ".$total_pages."</p>";
> $Goto = "Go to Page";
> $go = "Go";
>
>
> $texto = "
> <div id='paginacion'>".$toFrom."
> <ul>";
>
> if ($current > 1)
> $texto .= "<li><a href=\"$link$previous\">«</a></li>";
> else
> $texto .= "<li><b>«</b></li>";
>
>
> //Se lista hasta llegar a la posición actual
> if ($current > $limit) {
> $start = ($current - $limit) + 1;
> $texto .= "<li><b>...</b></li>";
> } else {
> $start = 1;
> }
>
> for ($start; $start < $current; $start++) {
> $texto .= "<li><a href=\"$link$start\">$start</a></li>";
> }
>
> //mostramos posicion actual
> $texto .= "<li><b>$current</b></li> ";
>
> //mostramos resto de registros
> for ($i=$current+1; $i<=$total_pages; $i++) {
>
> if ($i > $limit)
> {
> $texto .= "<li><b>...</b></li>";
> $i = $total_pages;
> }else{
> $texto .= "<li><a href=\"$link$i\">$i</a></li> ";
> }
> }
>
> if ($current < $total_pages)
> {
> $texto .= "<li><a href=\"$link$next\">»</a></li>";
> }else{
> $texto .= "<li><b>»</b></li>";
> }
>
> $texto .= "</ul>";
>
>
> //a form to go directly to the page you want
> $texto .= '<!--FORMULARIO PAGINADOR-->
> <div id="form_ir">
> <form action="'.$_SERVER["PHP_SELF"].'" method="get">
> <fieldset>
> <p>'.$Goto.' <input type="text" size="3" name="page" value="" />
> <input type="submit" name="ir" value="'.$go.'" /></p>
> </fieldset>
> </form>
> </div>';
>
>
>
> $texto .= '<br class="break" />
>
> </div>
> ';
>
> return $texto;
>
> }
>
> ?>
>
> //-------------------paginator.php------------------------------------------
> ----------------------------------
>
>
> //-------------------example.php--------------------------------------------
> --------------------------------
> <?php
> include ("paginator.php");
>
> for ($n=0; $n<=100; $n++)
> {
> $myArray [$n] = $n;
> }
>
> $total = count ($myArray); //the total elements in the array
> $per_page = 5; //elements the script show
> $link = "example.php?page="; //link
> $limit = 3; //number of items you show in the paginator list
>
>
>
> if ($_GET["page"])
> {
> $current = $_GET["page"];
> }else{
> $current = $_POST["page"];
> }
>
> $start = ($current-1) * $per_page;
> $end = $start + $per_page;
>
> //call to the paginator function ...
> echo paginator ($current, $total, $per_page, $link, $limit);
>
>
> //Show the results in different pages
> for ($i = $start; $i<=$end ;$i++)
> {
> echo $myArray[$i]."<br />";
> }
>
>
> ?>
>
>
> //-------------------example.php--------------------------------------------
> --------------------------------
>
>
> Thanks ruben very much :-)
>
>
>
> Regards
>
> Angelo
>
>
>
> http://www.wapit.co.za
> http://www.elemental.co.za
>
>
--
http://peachep.wordpress.com
--- End Message ---
--- Begin Message ---
On Sep 29, 2009, at 5:51 PM, Jim Lucas wrote:
Philip Thompson wrote:
On Sep 29, 2009, at 4:38 PM, Jim Lucas wrote:
Philip Thompson wrote:
On Sep 28, 2009, at 4:40 PM, jeff brown wrote:
Yes, that's the best way to clean up after yourself. And you
really
should use that on anything you have sitting around daemon like.
Jeff
Philip Thompson wrote:
On Sep 28, 2009, at 4:27 PM, Ralph Deffke wrote:
well this sound clearly to me like you are not freeing
resultsets
you are not going to use anymore. In long scripts you have to
take
care of this. on short scripts you can be a bit weak on that,
because the resultsets are closed and freed on script ending.
assumed u r using MySQL are u using mysql_free_result($result)
goog luck
[email protected]
"Philip Thompson" <[email protected]> wrote in message
news:[email protected]...
Hi all.
I have a script that opens a socket, creates a persistent mysql
connection, and loops to receive data. When the amount of
specified
data has been received, it calls a class which processes the
data
and
inserts it into the database. Each iteration, I unset/
destruct that
class I call. However, the script keeps going up in memory and
eventually runs out, causing a fatal error. Any thoughts on
where to
start to see where I'm losing my memory?
Thanks in advance,
~Philip
I am not using mysql_free_result(). Is that highly recommended
by all?
Thanks,
~Philip
I took your suggestions and made sure to clean up after myself. I'm
running into something that *appears* to be a bug with
mysql_free_result(). Here's a snippet of my db class.
<?php
class db {
function fetch ($sql, $assoc=false)
{
echo "\nMemory usage before query: " . number_format
(memory_get_usage ()) . "\n";
$resultSet = $this->query($sql);
echo "Memory usage after query: " . number_format
(memory_get_usage ()) . "\n";
if (!$assoc) { $result = $this->fetch_row($resultSet); }
else {
$result = $this->fetch_array($resultSet);
echo "Memory usage after fetch: " . number_format
(memory_get_usage ()) . "\n";
}
$this->freeResult($resultSet);
echo "Memory usage after free: " . number_format
(memory_get_usage ()) . "\n";
return $result;
}
function freeResult ($result)
{
if (is_resource ($result)) {
if (!mysql_free_result ($result)) { echo "Memory could
not
be freed\n"; }
}
unset ($result); // For good measure
}
function fetch_row ($set) {
return mysql_fetch_row ($set);
}
function fetch_array ($set) {
return mysql_fetch_array ($set, MYSQL_ASSOC);
}
}
// I seem to be losing memory when I call this
$db->fetch($sql);
?>
The result I get with this is...
Memory usage before query: 6,406,548
Memory usage after query: 6,406,548
Memory usage after fetch: 6,406,548
Memory usage after free: 6,406,572
As you may notice, the memory actually goes UP after the
*freeing* of
memory. Why is this happening?! What have I done wrong? Is this a
bug?
Any thoughts would be appreciated.
First off, my question would be, is your query actually working?
Because I
would imagine that if you were getting results back from the DB,
that
the amount
of memory being used would increase between step 1 & 2.
Check to make sure that you are getting results.
I'm confident the queries are working (there's many of them and I
know
the data they're returning is correct), but they may not always be
returning results. The memory value does change in some instances...
Memory usage before query: 5,138,372
Memory usage after query: 5,138,396
Memory usage after free: 5,138,556
This was one that use fetch_row() instead of fetch_array(), but the
same
difference. I did some searching around and I think it's a bug in PHP
and/or Zend Memory Management engine. As I mentioned in a previous
post
about mysql_query() not allocating memory appropriately, I believe
this
could quite possibly be the case. Several people have reported bugs
similar to this... unfortunately, they are marked as (erroneously?)
bogus or said it has been fixed (when, IMO, it has not).
http://bugs.php.net/bug.php?id=40883
http://bugs.php.net/bug.php?id=28424
http://bugs.php.net/bug.php?id=41871
I'm using version 5.2.6 on Fedora 8 and the bug still appears to be
there. I think I'm going to take a different approach to fix this.
I'll
create a shell script to loop and invoke the socket listener
script, and
when it gathers data, call the import script. This way, the php
script(s) will end execution after each iteration and release the
memory. Is this reasonable?
This has been a long day. Thanks for your input. Any more thoughts
are
welcome.
~Philip
Doesn't PHP only give back a "pointer" back from MySQL when it runs
a query?
Then what PHP does is uses that "pointer" to mysql to request the
next row of
results?
Couldn't you use the unbuffered-query function for this?
http://php.net/mysql-unbuffered-query
Maybe this will help?
I'm not exactly sure, Jim. I'll have to look into this. Thanks for the
suggestion.
~Philip
--- End Message ---
--- Begin Message ---
On Sep 29, 2009, at 6:15 PM, Eddie Drapkin wrote:
On Tue, Sep 29, 2009 at 6:51 PM, Jim Lucas <[email protected]> wrote:
Philip Thompson wrote:
On Sep 29, 2009, at 4:38 PM, Jim Lucas wrote:
Philip Thompson wrote:
On Sep 28, 2009, at 4:40 PM, jeff brown wrote:
Yes, that's the best way to clean up after yourself. And you
really
should use that on anything you have sitting around daemon like.
Jeff
Philip Thompson wrote:
On Sep 28, 2009, at 4:27 PM, Ralph Deffke wrote:
well this sound clearly to me like you are not freeing
resultsets
you are not going to use anymore. In long scripts you have to
take
care of this. on short scripts you can be a bit weak on that,
because the resultsets are closed and freed on script ending.
assumed u r using MySQL are u using mysql_free_result($result)
goog luck
[email protected]
"Philip Thompson" <[email protected]> wrote in message
news:[email protected]...
Hi all.
I have a script that opens a socket, creates a persistent
mysql
connection, and loops to receive data. When the amount of
specified
data has been received, it calls a class which processes the
data
and
inserts it into the database. Each iteration, I unset/
destruct that
class I call. However, the script keeps going up in memory and
eventually runs out, causing a fatal error. Any thoughts on
where to
start to see where I'm losing my memory?
Thanks in advance,
~Philip
I am not using mysql_free_result(). Is that highly recommended
by all?
Thanks,
~Philip
I took your suggestions and made sure to clean up after myself.
I'm
running into something that *appears* to be a bug with
mysql_free_result(). Here's a snippet of my db class.
<?php
class db {
function fetch ($sql, $assoc=false)
{
echo "\nMemory usage before query: " . number_format
(memory_get_usage ()) . "\n";
$resultSet = $this->query($sql);
echo "Memory usage after query: " . number_format
(memory_get_usage ()) . "\n";
if (!$assoc) { $result = $this->fetch_row($resultSet); }
else {
$result = $this->fetch_array($resultSet);
echo "Memory usage after fetch: " . number_format
(memory_get_usage ()) . "\n";
}
$this->freeResult($resultSet);
echo "Memory usage after free: " . number_format
(memory_get_usage ()) . "\n";
return $result;
}
function freeResult ($result)
{
if (is_resource ($result)) {
if (!mysql_free_result ($result)) { echo "Memory
could not
be freed\n"; }
}
unset ($result); // For good measure
}
function fetch_row ($set) {
return mysql_fetch_row ($set);
}
function fetch_array ($set) {
return mysql_fetch_array ($set, MYSQL_ASSOC);
}
}
// I seem to be losing memory when I call this
$db->fetch($sql);
?>
The result I get with this is...
Memory usage before query: 6,406,548
Memory usage after query: 6,406,548
Memory usage after fetch: 6,406,548
Memory usage after free: 6,406,572
As you may notice, the memory actually goes UP after the
*freeing* of
memory. Why is this happening?! What have I done wrong? Is this
a bug?
Any thoughts would be appreciated.
First off, my question would be, is your query actually working?
Because I
would imagine that if you were getting results back from the DB,
that
the amount
of memory being used would increase between step 1 & 2.
Check to make sure that you are getting results.
I'm confident the queries are working (there's many of them and I
know
the data they're returning is correct), but they may not always be
returning results. The memory value does change in some instances...
Memory usage before query: 5,138,372
Memory usage after query: 5,138,396
Memory usage after free: 5,138,556
This was one that use fetch_row() instead of fetch_array(), but
the same
difference. I did some searching around and I think it's a bug in
PHP
and/or Zend Memory Management engine. As I mentioned in a previous
post
about mysql_query() not allocating memory appropriately, I believe
this
could quite possibly be the case. Several people have reported bugs
similar to this... unfortunately, they are marked as (erroneously?)
bogus or said it has been fixed (when, IMO, it has not).
http://bugs.php.net/bug.php?id=40883
http://bugs.php.net/bug.php?id=28424
http://bugs.php.net/bug.php?id=41871
I'm using version 5.2.6 on Fedora 8 and the bug still appears to be
there. I think I'm going to take a different approach to fix this.
I'll
create a shell script to loop and invoke the socket listener
script, and
when it gathers data, call the import script. This way, the php
script(s) will end execution after each iteration and release the
memory. Is this reasonable?
This has been a long day. Thanks for your input. Any more thoughts
are
welcome.
~Philip
Doesn't PHP only give back a "pointer" back from MySQL when it runs
a query?
Then what PHP does is uses that "pointer" to mysql to request the
next row of
results?
Couldn't you use the unbuffered-query function for this?
http://php.net/mysql-unbuffered-query
Maybe this will help?
What version of PHP (older versions like to leak memory, if MY memory
serves me right, haha)? Have you tried running with 5.3.0 and using
the garbage collection?
5.2.6. I've not looked at 5.3, but I suppose it couldn't hurt to do
some testing. =D Thank you.
~Philip
--- End Message ---
--- Begin Message ---
Hi Philip,
before u start running arround and taking ur hand on major changes I would
like u to concider the following:
the way u use the memory_get_usage() is incomplete use the function like
memory_get_usage( true ) if this is set the REAL SIZE OF MEMORY ALLOCATED
FROM THE SYSTEM is shown.
some posters gave u little winks, like has the garbage collection been run?
freeing the results is important, it means the memory is available for other
data, but it does not necesarily mean it is reported just a step later as
free! u are running on concurrent os.
the only way u can test that is in real world. clean the whole application
with the free_result_set and see the effect.
I'm personaly not shure if the memory allocated for result sets for MySQL
are reported as used by PHP anyway. It might be that the amount of memory is
not shown as PHP used even if u set real_usage = true. it is something to
test.
I think the way u tested the effect of freeing the results is just wrong.
hope that helps
[email protected]
u came up with the problem that ur server is running out of memory.
"Philip Thompson" <[email protected]> wrote in message
news:[email protected]...
> On Sep 29, 2009, at 6:15 PM, Eddie Drapkin wrote:
>
> > On Tue, Sep 29, 2009 at 6:51 PM, Jim Lucas <[email protected]> wrote:
> >> Philip Thompson wrote:
> >>> On Sep 29, 2009, at 4:38 PM, Jim Lucas wrote:
> >>>
> >>>> Philip Thompson wrote:
> >>>>> On Sep 28, 2009, at 4:40 PM, jeff brown wrote:
> >>>>>
> >>>>>> Yes, that's the best way to clean up after yourself. And you
> >>>>>> really
> >>>>>> should use that on anything you have sitting around daemon like.
> >>>>>>
> >>>>>> Jeff
> >>>>>>
> >>>>>> Philip Thompson wrote:
> >>>>>>> On Sep 28, 2009, at 4:27 PM, Ralph Deffke wrote:
> >>>>>>>> well this sound clearly to me like you are not freeing
> >>>>>>>> resultsets
> >>>>>>>> you are not going to use anymore. In long scripts you have to
> >>>>>>>> take
> >>>>>>>> care of this. on short scripts you can be a bit weak on that,
> >>>>>>>> because the resultsets are closed and freed on script ending.
> >>>>>>>>
> >>>>>>>> assumed u r using MySQL are u using mysql_free_result($result)
> >>>>>>>>
> >>>>>>>> goog luck
> >>>>>>>>
> >>>>>>>> [email protected]
> >>>>>>>>
> >>>>>>>>
> >>>>>>>> "Philip Thompson" <[email protected]> wrote in message
> >>>>>>>> news:[email protected]...
> >>>>>>>>> Hi all.
> >>>>>>>>>
> >>>>>>>>> I have a script that opens a socket, creates a persistent
> >>>>>>>>> mysql
> >>>>>>>>> connection, and loops to receive data. When the amount of
> >>>>>>>>> specified
> >>>>>>>>> data has been received, it calls a class which processes the
> >>>>>>>>> data
> >>>>>>>>> and
> >>>>>>>>> inserts it into the database. Each iteration, I unset/
> >>>>>>>>> destruct that
> >>>>>>>>> class I call. However, the script keeps going up in memory and
> >>>>>>>>> eventually runs out, causing a fatal error. Any thoughts on
> >>>>>>>>> where to
> >>>>>>>>> start to see where I'm losing my memory?
> >>>>>>>>>
> >>>>>>>>> Thanks in advance,
> >>>>>>>>> ~Philip
> >>>>>>> I am not using mysql_free_result(). Is that highly recommended
> >>>>>>> by all?
> >>>>>>> Thanks,
> >>>>>>> ~Philip
> >>>>>
> >>>>> I took your suggestions and made sure to clean up after myself.
> >>>>> I'm
> >>>>> running into something that *appears* to be a bug with
> >>>>> mysql_free_result(). Here's a snippet of my db class.
> >>>>>
> >>>>> <?php
> >>>>> class db {
> >>>>> function fetch ($sql, $assoc=false)
> >>>>> {
> >>>>> echo "\nMemory usage before query: " . number_format
> >>>>> (memory_get_usage ()) . "\n";
> >>>>> $resultSet = $this->query($sql);
> >>>>> echo "Memory usage after query: " . number_format
> >>>>> (memory_get_usage ()) . "\n";
> >>>>>
> >>>>> if (!$assoc) { $result = $this->fetch_row($resultSet); }
> >>>>> else {
> >>>>> $result = $this->fetch_array($resultSet);
> >>>>> echo "Memory usage after fetch: " . number_format
> >>>>> (memory_get_usage ()) . "\n";
> >>>>> }
> >>>>>
> >>>>> $this->freeResult($resultSet);
> >>>>> echo "Memory usage after free: " . number_format
> >>>>> (memory_get_usage ()) . "\n";
> >>>>>
> >>>>> return $result;
> >>>>> }
> >>>>>
> >>>>> function freeResult ($result)
> >>>>> {
> >>>>> if (is_resource ($result)) {
> >>>>> if (!mysql_free_result ($result)) { echo "Memory
> >>>>> could not
> >>>>> be freed\n"; }
> >>>>> }
> >>>>> unset ($result); // For good measure
> >>>>> }
> >>>>>
> >>>>> function fetch_row ($set) {
> >>>>> return mysql_fetch_row ($set);
> >>>>> }
> >>>>>
> >>>>> function fetch_array ($set) {
> >>>>> return mysql_fetch_array ($set, MYSQL_ASSOC);
> >>>>> }
> >>>>> }
> >>>>>
> >>>>> // I seem to be losing memory when I call this
> >>>>> $db->fetch($sql);
> >>>>> ?>
> >>>>>
> >>>>> The result I get with this is...
> >>>>>
> >>>>> Memory usage before query: 6,406,548
> >>>>> Memory usage after query: 6,406,548
> >>>>> Memory usage after fetch: 6,406,548
> >>>>> Memory usage after free: 6,406,572
> >>>>>
> >>>>> As you may notice, the memory actually goes UP after the
> >>>>> *freeing* of
> >>>>> memory. Why is this happening?! What have I done wrong? Is this
> >>>>> a bug?
> >>>>> Any thoughts would be appreciated.
> >>>>>
> >>>>
> >>>> First off, my question would be, is your query actually working?
> >>>> Because I
> >>>> would imagine that if you were getting results back from the DB,
> >>>> that
> >>>> the amount
> >>>> of memory being used would increase between step 1 & 2.
> >>>>
> >>>> Check to make sure that you are getting results.
> >>>
> >>> I'm confident the queries are working (there's many of them and I
> >>> know
> >>> the data they're returning is correct), but they may not always be
> >>> returning results. The memory value does change in some instances...
> >>>
> >>> Memory usage before query: 5,138,372
> >>> Memory usage after query: 5,138,396
> >>> Memory usage after free: 5,138,556
> >>>
> >>> This was one that use fetch_row() instead of fetch_array(), but
> >>> the same
> >>> difference. I did some searching around and I think it's a bug in
> >>> PHP
> >>> and/or Zend Memory Management engine. As I mentioned in a previous
> >>> post
> >>> about mysql_query() not allocating memory appropriately, I believe
> >>> this
> >>> could quite possibly be the case. Several people have reported bugs
> >>> similar to this... unfortunately, they are marked as (erroneously?)
> >>> bogus or said it has been fixed (when, IMO, it has not).
> >>>
> >>> http://bugs.php.net/bug.php?id=40883
> >>> http://bugs.php.net/bug.php?id=28424
> >>> http://bugs.php.net/bug.php?id=41871
> >>>
> >>> I'm using version 5.2.6 on Fedora 8 and the bug still appears to be
> >>> there. I think I'm going to take a different approach to fix this.
> >>> I'll
> >>> create a shell script to loop and invoke the socket listener
> >>> script, and
> >>> when it gathers data, call the import script. This way, the php
> >>> script(s) will end execution after each iteration and release the
> >>> memory. Is this reasonable?
> >>>
> >>> This has been a long day. Thanks for your input. Any more thoughts
> >>> are
> >>> welcome.
> >>>
> >>> ~Philip
> >>>
> >>
> >> Doesn't PHP only give back a "pointer" back from MySQL when it runs
> >> a query?
> >> Then what PHP does is uses that "pointer" to mysql to request the
> >> next row of
> >> results?
> >>
> >> Couldn't you use the unbuffered-query function for this?
> >>
> >> http://php.net/mysql-unbuffered-query
> >>
> >> Maybe this will help?
> >
> > What version of PHP (older versions like to leak memory, if MY memory
> > serves me right, haha)? Have you tried running with 5.3.0 and using
> > the garbage collection?
>
> 5.2.6. I've not looked at 5.3, but I suppose it couldn't hurt to do
> some testing. =D Thank you.
>
> ~Philip
>
>
>
--- End Message ---