473,508 Members | 2,206 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Strange class OOP behavior

Anyone ever experienced this problem.

When i pass a mysql result to the constructor of a class and use that
resultset inside that class the original resultset outside the class gets
affected too. That is not right i think because my result inside my class is
private. Is this a bug? Can someone look at my code below please:

<?php

require_once('includes/connect.php');

class test {

private $_result;
private $_record;

public function __construct($result) {

$this->_result = $result;

}

public function show() {

while($this->_record = mysql_fetch_array($this->_result)) {

echo $this->_record['name'].'<br />';

}

}

}
# usage
# sql statement dat artikels ophaald uit cartal database
$SQL = "SELECT * FROM test_table";

# uitvoeren van dit statement
$RESULT = mysql_query($SQL,$conn);

$t = new test($RESULT);

$t->show();

// mysql_data_seek($RESULT,0);

// this prints out nothing unless i call mysql_data_seek($RESULT,0) before
this whileloop so that means that the pointer of the recordset had moved to
the end because of the while loop in the class itself...that is strange!
while($RECORD = mysql_fetch_array($RESULT)) {

echo $RECORD['name'].'<br />';

}

?>
Aug 27 '07 #1
5 1583
On Aug 27, 10:25 am, "Marcel Molenaar" <afraidofs...@spam.nlwrote:
Anyone ever experienced this problem.

When i pass a mysql result to the constructor of a class and use that
resultset inside that class the original resultset outside the class gets
affected too. That is not right i think because my result inside my class is
private. Is this a bug? Can someone look at my code below please:

<?php

require_once('includes/connect.php');

class test {

private $_result;
private $_record;

public function __construct($result) {

$this->_result = $result;

}

public function show() {

while($this->_record = mysql_fetch_array($this->_result)) {

echo $this->_record['name'].'<br />';

}

}

}

# usage
# sql statement dat artikels ophaald uit cartal database
$SQL = "SELECT * FROM test_table";

# uitvoeren van dit statement
$RESULT = mysql_query($SQL,$conn);

$t = new test($RESULT);

$t->show();

// mysql_data_seek($RESULT,0);

// this prints out nothing unless i call mysql_data_seek($RESULT,0) before
this whileloop so that means that the pointer of the recordset had moved to
the end because of the while loop in the class itself...that is strange!
while($RECORD = mysql_fetch_array($RESULT)) {

echo $RECORD['name'].'<br />';

}

?>
The result is a resource, so there's only one of them to pass around.
It doesn't make a copy of it when you pass it into the class.

Aug 27 '07 #2
On 27.08.2007 16:25 Marcel Molenaar wrote:
Anyone ever experienced this problem.

When i pass a mysql result to the constructor of a class and use that
resultset inside that class the original resultset outside the class gets
affected too. That is not right i think because my result inside my class is
private. Is this a bug?
No, this is the expected behaviour. Resources, like db connections or
result handles are always assigned and passed as pointers, that is,
changing one variable will affect another one if both point to the same
object.
--
gosha bine

makrell ~ http://www.tagarga.com/blok/makrell
php done right ;) http://code.google.com/p/pihipi
Aug 27 '07 #3

"ZeldorBlat" <ze********@gmail.comwrote in message
news:11*********************@y42g2000hsy.googlegro ups.com...
On Aug 27, 10:25 am, "Marcel Molenaar" <afraidofs...@spam.nlwrote:
>Anyone ever experienced this problem.

When i pass a mysql result to the constructor of a class and use that
resultset inside that class the original resultset outside the class gets
affected too. That is not right i think because my result inside my class
is
private. Is this a bug? Can someone look at my code below please:

<?php

require_once('includes/connect.php');

class test {

private $_result;
private $_record;

public function __construct($result) {

$this->_result = $result;

}

public function show() {

while($this->_record = mysql_fetch_array($this->_result)) {

echo $this->_record['name'].'<br />';

}

}

}

# usage
# sql statement dat artikels ophaald uit cartal database
$SQL = "SELECT * FROM test_table";

# uitvoeren van dit statement
$RESULT = mysql_query($SQL,$conn);

$t = new test($RESULT);

$t->show();

// mysql_data_seek($RESULT,0);

// this prints out nothing unless i call mysql_data_seek($RESULT,0)
before
this whileloop so that means that the pointer of the recordset had moved
to
the end because of the while loop in the class itself...that is strange!
while($RECORD = mysql_fetch_array($RESULT)) {

echo $RECORD['name'].'<br />';

}

?>

The result is a resource, so there's only one of them to pass around.
It doesn't make a copy of it when you pass it into the class.
Thanks!

But is it possible to copy it into a new temporary resource so the original
result will stay untouched?

Marcel
Aug 27 '07 #4
Marcel Molenaar wrote:
"ZeldorBlat" <ze********@gmail.comwrote in message
news:11*********************@y42g2000hsy.googlegro ups.com...
>On Aug 27, 10:25 am, "Marcel Molenaar" <afraidofs...@spam.nlwrote:
>>Anyone ever experienced this problem.

When i pass a mysql result to the constructor of a class and use that
resultset inside that class the original resultset outside the class gets
affected too. That is not right i think because my result inside my class
is
private. Is this a bug? Can someone look at my code below please:

<?php

require_once('includes/connect.php');

class test {

private $_result;
private $_record;

public function __construct($result) {

$this->_result = $result;

}

public function show() {

while($this->_record = mysql_fetch_array($this->_result)) {

echo $this->_record['name'].'<br />';

}

}

}

# usage
# sql statement dat artikels ophaald uit cartal database
$SQL = "SELECT * FROM test_table";

# uitvoeren van dit statement
$RESULT = mysql_query($SQL,$conn);

$t = new test($RESULT);

$t->show();

// mysql_data_seek($RESULT,0);

// this prints out nothing unless i call mysql_data_seek($RESULT,0)
before
this whileloop so that means that the pointer of the recordset had moved
to
the end because of the while loop in the class itself...that is strange!
while($RECORD = mysql_fetch_array($RESULT)) {

echo $RECORD['name'].'<br />';

}

?>
The result is a resource, so there's only one of them to pass around.
It doesn't make a copy of it when you pass it into the class.

Thanks!

But is it possible to copy it into a new temporary resource so the original
result will stay untouched?

Marcel

You can make a copy, but since this isn't your actual data, but a
reference to data residing in MySQL, it won't do any good.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Aug 27 '07 #5

"Jerry Stuckle" <js*******@attglobal.netwrote in message
news:VM******************************@comcast.com. ..
Marcel Molenaar wrote:
>"ZeldorBlat" <ze********@gmail.comwrote in message
news:11*********************@y42g2000hsy.googlegr oups.com...
>>On Aug 27, 10:25 am, "Marcel Molenaar" <afraidofs...@spam.nlwrote:
Anyone ever experienced this problem.

When i pass a mysql result to the constructor of a class and use that
resultset inside that class the original resultset outside the class
gets
affected too. That is not right i think because my result inside my
class is
private. Is this a bug? Can someone look at my code below please:

<?php

require_once('includes/connect.php');

class test {

private $_result;
private $_record;

public function __construct($result) {

$this->_result = $result;

}

public function show() {

while($this->_record = mysql_fetch_array($this->_result)) {

echo $this->_record['name'].'<br />';

}

}

}

# usage
# sql statement dat artikels ophaald uit cartal database
$SQL = "SELECT * FROM test_table";

# uitvoeren van dit statement
$RESULT = mysql_query($SQL,$conn);

$t = new test($RESULT);

$t->show();

// mysql_data_seek($RESULT,0);

// this prints out nothing unless i call mysql_data_seek($RESULT,0)
before
this whileloop so that means that the pointer of the recordset had
moved to
the end because of the while loop in the class itself...that is
strange!
while($RECORD = mysql_fetch_array($RESULT)) {

echo $RECORD['name'].'<br />';

}

?>
The result is a resource, so there's only one of them to pass around.
It doesn't make a copy of it when you pass it into the class.

Thanks!

But is it possible to copy it into a new temporary resource so the
original result will stay untouched?

Marcel

You can make a copy, but since this isn't your actual data, but a
reference to data residing in MySQL, it won't do any good.

--
Thanks for your help!
Aug 28 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1137
by: John Hunter | last post by:
I have a class that uses some extension code I have written and I am trying to track down some memory leaks, which I presume to be in my extension code. The class is question is in a python...
7
1695
by: Anon Email | last post by:
Hi people, I'm playing around with Bartosz Milewski's code at the moment, and I got the following strange results upon execution of the code included further below. Please be aware that I...
9
1641
by: Karahan Celikel | last post by:
Here are three simple classes: class A { public void DoIt(B b) { DoSomething(b); } public void DoSomething(B b) {
3
1839
by: Arnold Schrijver | last post by:
I wrote a program that draws items to the screen and maintains a set of Offset values. There was a bug in the code, because objects were positioned wrongly. While debugging I found some peculiar...
7
1511
by: Paul Czubilinski | last post by:
Hello, I have a problem with code like this (PHP 5.1.4): fila A.php: ======= include(B.php); class document extends obj { .........
9
1156
by: M. Posseth | last post by:
i have 3 forms Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim frm As New Form2 frm.Show(Me) End Sub...
3
2416
by: senfo | last post by:
I developed a Windows control in VS 2005 that inherits from the PictureBox Control that adds the ability to select images in a Windows application. It is, however, experiencing a strange issue...
5
1490
by: rconradharris | last post by:
A co-worker of mine came across some interesting behavior in the Python interpreter today and I'm hoping someone more knowledgeable in Python internals can explain this to me. First, we create...
1
265
by: Marcel Molenaar | last post by:
Anyone ever experienced this problem. When i pass a mysql result to the constructor of a class and use that resultset inside that class the original resultset outside the class gets affected...
0
7225
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7123
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7326
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7383
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7498
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4707
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3194
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
766
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
418
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.