简体   繁体   中英

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:

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(......) . If thats true, then you are feeding the userData with a Resource string which is a resource data type.

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;

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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