简体   繁体   English

如何使用 PHP 从 MySQL 获取记录到值对象中

[英]How to get records from MySQL into value objects with PHP

I have this code:我有这个代码:

$Result = DBQuery("SELECT user_id,nick_name,email FROM users LIMIT $fromindex.",".$numOfUsers);
if ($Result)
{
     $resultUsers->UersData=$Result;
}

The problem is that the results U get are in some kind of format that i don't know.问题是你得到的结果是某种我不知道的格式。

I want to get the results in array format, which every element of that array should be a value object class of this type:我想以数组格式获取结果,该数组的每个元素都应该是这种类型的值 object class :

class UserVO {
    var $_explicitType="UserVO";

    var $id;

    var $nickName;

    var $email;
}

Any idea how it can be done?知道怎么做吗?

My answer might not sound very helpful, regarding your unclear question.关于您不清楚的问题,我的回答可能听起来不是很有帮助。 I am supposing, that the function DBquery returns the result of mysql_query(......) .我假设 function DBquery返回mysql_query(......)的结果。 If thats true, then you are feeding the userData with a Resource string which is a resource data type.如果那是真的,那么您正在为userData提供一个Resource string ,它是一种资源数据类型。

There are few ways to access such resource String访问此类资源字符串的方法很少

  1. If the query exports multiple results如果查询导出多个结果

    while($row = mysql_fetch_assoc($userData) { print_r($row); //This is where you will get your mysql rows. }
  2. If the query return single row如果查询返回单行

    $row = mysql_fetch_assoc($userData); print_r($row); // //This is where you will get your mysql rows.

Here are some must check links这里有一些必须检查的链接

Build your DBQuery upon PDO and set the flag PDO::FETCH_CLASS as documented here;在 PDO 上构建您的 DBQuery 并设置标志 PDO::FETCH_CLASS,如此处所述;

http://php.net/manual/en/pdostatement.fetch.php http://php.net/manual/en/pdostatement.fetch.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM