By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,831 Members | 2,237 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,831 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.