0

I have a table in a MySQL table called persons

id  LastName    FirstName
1       Hansen      Timoteivn
2       Svendson    Tove
3       Pettersen   Kari

and another MySQL table called orders.

id  OrderNo  personID
1       77895    3
2       44678    3
3       22456    1
4       24562    1
5       34764    15

How can I write a SQL query that I feed into PHP's mysql_query() function to return a list of "Order objects" that each contain a "Person object?" Each "Person object" has first name and last name as properties.

4
  • 6
    SQL doesn't give you objects. Ever. Commented Mar 28, 2012 at 3:30
  • Is there a way one could use mysql_fetch_object() to produce an object with objects from a resource returned by a SQL query? Commented Mar 28, 2012 at 3:35
  • you want to use that object for showing in front end screen only . right ? Commented Mar 28, 2012 at 3:46
  • Yes, I want to be able to iterate through an array of Order objects, and list order numbers next to the first and last names of persons. Commented Mar 28, 2012 at 3:57

1 Answer 1

2

this query will return orders by a certain person (this will not give the object)

SELECT a.ID, a. FirstName, a.LastName, b.OrderNo
FROM Persons a INNER JOIN Orders b ON
        a.ID = b.PersonID
WHERE a.ID = 1
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.