English 中文(简体)
如何从PHP的一个价值物体获取数据?
原标题:How to get the data from a value object in PHP?
  • 时间:2011-02-25 22:33:23
  •  标签:
  • php
  • oop

因此,我正在从PHP的一个数据库获取数据。 我打算比较从询问中获得的数据中得出的每个物体的数值。 迄今情况良好。

当我试图在物体创建后从物体中提取数据时,这个问题就产生了。

我不是PHP dev,因此我不知道我是否遵循适当的PHP逻辑。 我用于联合材料、AS3和Java,因此,物体和价值物体与我所了解的情况相比,在购买力平价中是很小的。

任何人都知道如何检索我的数据?

<?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 __construct()
        {
            $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++;
    }
?>
最佳回答

<代码> 用户[] = 用户;

这应当做到:

<代码>用户[] = 美元;

这是你将新的用户表示反对的变量。

如果你制造物体,你也应当有母子。

http://www.ohchr.org。

问题回答

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;

然后,你也可以使用<代码>foreach(>,在用户阵列上,而不是在下方使用。

我想写一下法典的最后部分,如:

$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;
}

那么,你就能够做到:

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

Hmm,我可以看到几个问题:

  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. 你们是否需要为此设立一个类别? 除非你再做大量繁重的工作,否则你很可能会放弃多维阵列......我sql_fetch_assoc()对此非常重要,因为这会创造出一系列相关的表格。 我通常做的是:


$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;
   }
}

之后,我做了一些整理,然后用了以下文字:


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

   echo "$uid $name $email"; 
}





相关问题
Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

JSON with classes?

Is there a standardized way to store classes in JSON, and then converting them back into classes again from a string? For example, I might have an array of objects of type Questions. I d like to ...

Object-Oriented Perl constructor syntax and named parameters

I m a little confused about what is going on in Perl constructors. I found these two examples perldoc perlbot. package Foo; #In Perl, the constructor is just a subroutine called new. sub new { #I ...

Passing another class amongst instances

I was wondering what is the best practice re. passing (another class) amongst two instances of the same class (lets call this Primary ). So, essentially in the constructor for the first, i can ...

Where can I find object-oriented Perl tutorials? [closed]

A Google search yields a number of results - but which ones are the best? The Perl site appears to contain two - perlboot and perltoot. I m reading these now, but what else is out there? Note: I ve ...

热门标签