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.