473,498 Members | 1,724 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How do I retrieve my array from mysql_fetch_array when returned froma class?

In my code I cannot figure out how to retrieve multple rows from my
returned array from a class method. I have tried:

<?php
class myClass
{
private $connection;

/* Class constructor */
function myClass(){
/* Make connection to database */
$this->connection = mysql_connect("localhost", "myuser",
"secret") or die(mysql_error());
mysql_select_db("users", $this->connection) or
die(mysql_error());
}

function getUserInfo(){
$q = "SELECT * FROM users";
$result = mysql_query($q, $this->connection);
/* Error occurred, return given name by default */
if(!$result || (mysql_numrows($result) < 1)){
return ' no data ';
}
/* Return result array */
$dbarray = mysql_fetch_array($result);
return $dbarray;

}

}

$database = new myClass();
$mytest = $database->getUserInfo();

while ($mytest){
echo $mytest[0]; //breaks bad; infinite loop, don't know why.
}

echo "done";

?>

I tried:

foreach ($mytest as $items)
{
echo $mytest[0];
}

But it just prints out the same row four times (I only have four
rows). I don't know why it doesn't move to the next row.

I then tried:

$mytest = array();

$mytest = $database->getUserInfo();

foreach ($mytest as $key =$value){

echo '</brkey: ' . $key . ' value: ' . $value . '</br>';
}

but got only the first row again:

start
key: 0 value: 2

key: id value: 2

key: 1 value: test at row 2

key: username value: test at row 2

key: 2 value: email

key: email value: email
done

How do I get it to move to the next row? Thank you for any help.
Sep 16 '08 #1
5 6620
jmDesktop schreef:
while ($mytest){
echo $mytest[0]; //breaks bad; infinite loop, don't know why.
}
This is not the way to get items from an array; typically, you would do
something as follows:

foreach ($mytest as $value) {
print $value;
}

However, this will return a single row. If you want all results, you can
either have the function return a result on which you can apply
mysql_fetch_array outside the class, or implement an iterator:

class myClass {

private $_result = null;
...

function getUserInfo(){
$q = "SELECT * FROM users";
$_result = mysql_query($q, $this->connection);
}

function fetchRow() {
return mysql_fetch_array($_result);
}
}

$database = new myClass();
$database->getUserInfo();

while ($row = $database->fetchRow){
foreach ($row as $value) print $value;
}
JW
Sep 16 '08 #2
On Sep 16, 10:46*am, jmDesktop <needin4mat...@gmail.comwrote:
In my code I cannot figure out how to retrieve multple rows from my
returned array from a class method. *I have tried:

<?php
class myClass
{
* * * * private $connection;

* * /* Class constructor */
* *function myClass(){
* * * /* Make connection to database */
* * * $this->connection = mysql_connect("localhost", "myuser",
"secret") or die(mysql_error());
* * * mysql_select_db("users", $this->connection) or
die(mysql_error());
* * *}

* *function getUserInfo(){
* * * $q = "SELECT * FROM users";
* * * $result = mysql_query($q, $this->connection);
* * * /* Error occurred, return given name by default */
* * * if(!$result || (mysql_numrows($result) < 1)){
* * * * *return ' no data ';
* * * }
* * * /* Return result array */
* * * $dbarray = mysql_fetch_array($result);
* * * return $dbarray;

* *}

}

$database = new myClass();
$mytest = $database->getUserInfo();

while ($mytest){
* echo $mytest[0]; //breaks bad; infinite loop, don't know why.

}

echo "done";

?>

I tried:

foreach ($mytest as $items)
{
* echo $mytest[0];

}

But it just prints out the same row four times (I only have four
rows). *I don't know why it doesn't move to the next row.

I then tried:

$mytest = array();

$mytest = $database->getUserInfo();

foreach ($mytest as $key =$value){

* echo '</brkey: ' . $key . ' value: ' . $value . '</br>';

}

but got only the first row again:

start
key: 0 value: 2

key: id value: 2

key: 1 value: test at row 2

key: username value: test at row 2

key: 2 value: email

key: email value: email
done

How do I get it to move to the next row? *Thank you for any help.
I ended up using:

/* Return result array */
//$dbarray = mysql_fetch_array($result); //old
while($row = mysql_fetch_array($result)) $dbarray[] =
$row; //load array //new
return $dbarray;

and then:

$database = new myClass();
$mytest = $database->getUserInfo();

foreach ($mytest as $item){

echo '</brdata: ' . $item[1] . '</br>';
}
Sep 16 '08 #3
jmDesktop schreef:
I ended up using:

/* Return result array */
//$dbarray = mysql_fetch_array($result); //old
while($row = mysql_fetch_array($result)) $dbarray[] =
$row; //load array //new
return $dbarray;
This can slow down your script quite a bit with large data sets.
JW
Sep 17 '08 #4
On Sep 17, 3:31*am, Janwillem Borleffs <j...@jwscripts.comwrote:
jmDesktop schreef:
I ended up using:
* * /* Return result array */
* * * * *//$dbarray = mysql_fetch_array($result); //old
* * * * *while($row = mysql_fetch_array($result)) $dbarray[] =
$row; *//load array //new
* * * * *return $dbarray;

This can slow down your script quite a bit with large data sets.

JW
Is that just because in your example you keep the returned array
pointed to by the object and access it with $database->fetchrow as
opposed to building the array and then sending the array? In my
example I'm sending an entire dataset versus a one that is already
built and pointed to?
Sep 17 '08 #5
jmDesktop schreef:
Is that just because in your example you keep the returned array
pointed to by the object and access it with $database->fetchrow as
opposed to building the array and then sending the array? In my
example I'm sending an entire dataset versus a one that is already
built and pointed to?
Exactly. What you are doing is storing the entire result in memory
through an array. When using an iterator, you are retrieving one row at
a time.

Especially when you'r only printing the rows, this method is much more
effecient.
JW
Sep 17 '08 #6

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

Similar topics

10
1424
by: CLEAR-RCIC | last post by:
How would I create an array of objects inside a class? When I do it the following way I get an error that says: 'Object reference not set to an instance of an object'. Public Class Inmate...
3
1357
by: John Devlon | last post by:
Hi does anyone know how to create an array as property of a class ? can such an array be redimed ? Thanx John
2
2056
by: Fredrik Strandberg | last post by:
I have not been able to find the solution of this problem anywhere: I am building a class PrivateHelper that provides methods to access private members and invoke private methods, to be used for...
4
1574
by: hweekuan | last post by:
Hi, I got an error with gnu C++ but not with intel C++ (icc). The distilled code is given below, a variable "T" is stored as a const T& in the base class and when the derived class try to access...
6
1334
by: lwoods | last post by:
I want to be able to get the present year in one statement. I know that I can do it this way: $y = getdate(); echo 'year='.$y; But, I want to do something like: echo 'year='.getdate(); ...
1
1638
by: Jeroen | last post by:
Hello, Is it possible to retrieve the date when a record is created in an Access database table? And if so, how can I do that (by code)? Thanks in advance, Jeroen Elias
2
2467
by: tony | last post by:
Hello!! I know what an abstract class is which mean that the one of the derived class must define the abstract methods in the abstract class. So all the abstract methods in the abstarct class...
0
1896
by: devil | last post by:
I have 2 classes, class X, class Y: class Y { ClassX array; } When class y is instrantiated: YY:array(val1,val2),
1
1241
by: mmatchyn | last post by:
Is there a way to pass an object when including a class? For example... namespace testclass { public System.Windows.Forms.TextBox textbox; public class Write { public Write(string msg)
0
7005
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
7168
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
7210
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
7381
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...
1
4916
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4595
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
3096
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...
0
3087
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
293
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.