开发者

How to get the data from a value object in PHP?

So i am getting data from a database in PHP. I intend to compare values from each object created from the data obtained from the query. So far so good.

The issue arises when I try to retrieve the data from the objects once they are created, it turns out empty.

I am not a PHP dev, so I do not know if I follow a proper PHP logic. I am used to JS, AS3 and Java, so objects and value objects are a bit different in PHP than from what I know.

Anybody knows how i can retrieve my data?

<?php
    include("../config.php");

    class userVO
    {
        public  $uid;
        public  $name;
        public  $email;
        public  $list;
        public  $num_list_items;
        public  $matches;
        public  $num_matches;

        public function __constr开发者_如何转开发uct()
        {
            $this->matches = array();
        }
    }

    mysql_connect(DB_HOST, DB_USER, DB_PASSWORD) or die(mysql_error());
    mysql_select_db(DB_NAME) or die(mysql_error());

    $json_array = array();
    $result = mysql_query("...");

    $num_results = mysql_numrows($result);
    mysql_close();

    $users = array();
    $i = 0;
    while($i < $num_results)
    {
        $match = new userVO;
        $match->uid = mysql_result($result, $i, "uid");
        $match->name = mysql_result($result, $i, "name");
        $match->email = mysql_result($result, $i, "email");
        $users[] = userVO;
        $i++;
    }

    $num_users = count($users);
    echo "num users: " . $num_users . "<br>";

    $i = 0;
    while($i < $num_users)
    {
        echo "--- i: " . $i . " ---<br>";
        $current_user = $users[$i];

        echo "users[" . $i . "]: " . $users[$i] . "<br>";
        echo "users[" . $i . "]->name: " . $users[$i]->name . "<br>";
        echo "current user: " . $current_user . "<br>";
        echo "current user name: " . $current_user->name . "<br>";

        $i++;
    }
?>


You have

$users[] = userVO;

This should be

$users[] = $match;

which is the variable you put the new userVO object in.

You should also have parentheses where you create the object

$match = new userVO();


One thing that I see that doesn't look right is this line here in the while loop: $users[] = userVO;, I think that should be $users[] = $match;

You can then also use a foreach() loop on the $user's array instead of another while down below.

I would write the last portion of the code like:

$result = mysql_query("...");

$users = array();

while($row = mysql_fetch_assoc($result))
{
    $match = new userVO();
    $match->uid = $row["uid"];
    $match->name = $row["name"];
    $match->email = $row["email"];
    $users[] = $match;
}
mysql_close();

$num_users = count($users);
echo "num users: " . $num_users . "<br>";

foreach($users as $key => $user)
{
    echo "--- key: " . $key . " ---<br>";
    echo "users[" . $key . "]: " . $user . "<br>";
    echo "users[" . $key . "]->name: " . $user->name . "<br>";
    echo "current user: " . $user . "<br>";
    echo "current user name: " . $user->name . "<br>";
}


?>

Oh, also, with a bit of a change to the __construct function you can do this:

public function __construct($uid, $name, $email)
{
    $this->matches = array();
    $this->uid = $uid;
    $this->name = $name;
    $this->email = $email;
}

and then you can just do:

while($row = mysql_fetch_assoc($result))
{
    $users[] = new userVO($row["uid"],$row["name"],$row["email"]);
}


Hmm, well I can see a few problems right off:

  1. $users[] = userVO; You're not adding $match to the array, you're adding userVO, which is the name of the class, not the variable holding the instance of the class.

  2. Do you need to create a class for this? Unless you're going to be doing a lot of heavy work with these, you could probably get away with multi-dimensional arrays... mysql_fetch_assoc() is great for this, as it creates an associative array of your table row. What I do is typically something like:


$sql = "SELECT * FROM table WHERE foo='bar'";
$result = mysql_query($sql);

if (!$result) {
   echo 'Could not run query: ' . mysql_error();
   echo 'Query was: ' . $sql;
   exit;
}

$stuff = array();
if (mysql_num_rows($result) > 0) {
   while ($row = mysql_fetch_assoc($result)) {
      $stuff[] = $row;
   }
}

Then once I'm done collating pieces, I do the following to use it:


foreach($stuff as $row){
   $uid = $row['uid'];
   $name = $row['name'];
   $email = $row['email'];

   echo "$uid $name $email"; 
}

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜