I assume the answer is simple but I don't know how to do it with an object but I'm merging the result from this with another object. I know how to do it with an array easy enough but can't figure this one out, keep getting the error "Attempt to assign property of non-object". Help is appreciated.

I had been using the below code to return false if nothing is returned from a database. It SEEMED to be working but I now realize it isn't, as in the above code, it goes to the ELSE even if there are no rows returned. What is a good way to quickly check if a result has been returned or not?

if(!$result = mysql_query("QUERY"))

Thanks for reading guys and gals. Coding, the joy that keeps on giving.

chris_upjohn
—
2011-08-14T02:02:39Z —
#2

Object are a tricky one, i had the same troubles at first but after a quick search i found the answer i needed. Basically what you have is trying to reference a class which doesn't exist, to set the object property all we need to do is use the curly braces {} and it will work.

$userMeta->{$field} = $data;

Using the code you have won't count the results, what it's doing is checking if he MySQL query has successfully run or not. so for instance this is how you would write the above code to check for errors.

$userMeta has to have a default value of false not object or it will error out, also please read back through my post about the IF statement you have as at the moment its not correct as per error catching goes.

darkwarrior
—
2011-08-14T23:50:36Z —
#8

SgtLegend said:

$userMeta has to have a default value of false not object or it will error out, also please read back through my post about the IF statement you have as at the moment its not correct as per error catching goes.

Thanks, setting it to FALSE worked.

As per your other suggestion, from a user-friendliness bent, wouldn't having the SQL just die be a bad thing?

chris_upjohn
—
2011-08-15T00:05:20Z —
#9

Not necessarily, if you want a silent error for production that's fine but for development you should always use either exit or die to ensure that your script contains no errors. Personally i do this all the time as its good practice.

darkwarrior
—
2011-08-20T19:40:42Z —
#10

Ah, OK, I see your point then. I do intend to build an error function for the full implementation to catch stuff like that without just halting the process.

ahundiak
—
2011-08-21T02:39:40Z —
#11

I know this is not quite what you asked but here is an example of querying a games table that has a one to many relationship with the teams playing in the game as well as a one to many relationship to the referees.

The point being that if you really want to start using objects that can be persisted to a database then consider reading through some of the various ORM documentation for ideas. Doctrine2 happens to be the latest and some would say greatest. Maybe start by looking at the query chapter:13. Doctrine Query Language — Doctrine 2 ORM v2.1 documentation

darkwarrior
—
2011-09-30T23:30:47Z —
#12

Having a similar problem again.

I tried variations of the above when trying to retrieve multiple unknown rows from a database into an object but am having no success.

But that only returns one row. Maybe I'm going about it wrong, just getting annoyed with having to assign the results into an array to return it and then having to do a foreach to access it in circumstances where I'm only expecting one result.

Am I being dumb about this?

ahundiak
—
2011-10-01T00:59:00Z —
#13

$comList[] = $row;Assuming that $comList is an array.And add error_reporting(E_ALL); to the top of your page. It will help pick up a bunch of questionable stuff.

SpacePhoenix
—
2011-10-01T06:04:31Z —
#14

When your using a while loop to get all rows of a result set, set up the variable that your going to use as an array before the while loop runs then if you don't have any matches any foreach loop that you've got set to act on the result set want give any errors if the result set is empty.

If there are no rows returned by whatever query is being used then any foreach loop set to iterate through the result set will see what it expects, an array, albeit an empty array.

darkwarrior
—
2011-10-02T22:18:59Z —
#15

My error catching is set to Max already.

So the only way to do it with an object is by placing it in an array unless you know what results to expect?

I was able to get success when assigning it as "$object->{$columnname} = $data"

Where $columnname is the name of hte database column returned and $data is the contents of the row returned.

But if I do it as $object->{}, it throws an error.

ahundiak
—
2011-10-02T23:54:24Z —
#16

You might want to invest some time reading the section on classes and objects in the manual. Squiggly brackets {} are not directly related to object oriented programing. In fact, $object->$columnname = $data would work just as well.