I'm new to PHP. I've created classes that represent my database tables (I come from a .Net entity framework background). I would like to run my SELECT statement and pass back a User
object. All the examples I've found parse the returned data. Is there a way to just do something like this and have it pass back a User
object:
$user = new user();
$user = $mysqli->query($query, MYSQLI_STORE_RESULT);
return $user;
or do I have to parse the result first?
Note: all the fields in the user class match the fields in the database I.E. $user->UserName = [user].[UserName]
-
you mean you want to get them as objects? php.net/manual/en/mysqli-result.fetch-object.phpmeWantToLearn– meWantToLearn2012年08月07日 18:43:18 +00:00Commented Aug 7, 2012 at 18:43
-
Hope you can go back to .NET asap, it's a better world! :PMarcelo Assis– Marcelo Assis2012年08月07日 18:47:43 +00:00Commented Aug 7, 2012 at 18:47
1 Answer 1
Try use PDO with "fetchobject":
$statement = $db->query($query);
$user = $statement->fetchObject('User');
http://www.php.net/manual/en/pdostatement.fetchobject.php
or convert (cast) each "row" of data to User Object (example function)
or try a ORM PHP solution