May 1, 2011

How to get records from MySQL into value objects with PHP

Question by user733284

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?

Answer by Starx

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

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!