[Solved] Undefined property: stdClass::$DATA

Hi all

I have an older script which I’m slightly refactoring, though now its showing an error I can’t quite figure why.

Notice : Undefined property: stdClass::$DATA in /home/… on line 95

Can anybody see what I’m doing wrong?

$stmt = $mysqli->query("
SELECT CONCAT(event_id,venue_id) AS link
    , DATE_FORMAT(date, '%M %D %Y') AS drlist
    , date
    , about
    , photo
    FROM gallery_details
    ORDER BY gallery_details.date DESC");

    $row_cnt = $stmt->num_rows;
    $set = array();

    while ($record = mysqli_fetch_object($stmt)) {
      $record->e_link = str_replace(" ","-",($record->link)); // Line 95
      $set[$record->DATA][] = $record;
    }
...
}

Further down the page (this works ok):

foreach ($set as $DATA => $records) {
            foreach ($records as $record) {
              print "<li>\n";
              print '<a href="photos/'. $record->e_link . '/' . $record->date . '">' . $record->about . '<br /><span>' . $record->drlist;
              print "</span></a></li>\n";
            }
          }

Thanks, Barry

what are you trying to access with this? you dont select anything named data in your query?

Did you perhaps typo ‘date’?

Your query does not have a column called DATA so $record->DATA obviously does not exist. What is $record->DATA supposed to be? date maybe? drlist perhaps?

Or maybe you meant something like:

$set['DATA'][] = $record;

Which would be a bit strange.

I’m not really sure :thinking:
As mentioned, this is an older script and from what I can remember I needed to add this because of some CONCAT - though I could be wrong.

Would you say it’s safe to remove? (If its doing nothing)

I did think it was conected to:
foreach ($set as $DATA => $records) {

And thanks @ahundiak
Yes this does seem strange, not sure.

If you’re only using $set in this one place, i don’t see a need for the reference. Or even the multidimensional array. Were you pulling data from multiple queries for some reason?

You might be right. Again, only a vague understanding of my previous issues.
What would be your suggestion on refactoring this?

Yes. The query above is only fired if the date variable has no value, else I run another query.

if(isset($date)) {
$stmt = $mysqli->prepare("
    SELECT
    ...
")
}

if(!isset($date)) {
// The query above goes here
}

Well, i’m sure certain individuals will be along momentarily to suggest you scrap the entire thing and ‘figure out your real problem’, but for a simple refactor…

$set[$record->DATA][] = $record;
=>
$set[] = $record;
, and

foreach ($set as $DATA => $records) {
            foreach ($records as $record) {
              print "<li>\n";
              print '<a href="photos/'. $record->e_link . '/' . $record->date . '">' . $record->about . '<br /><span>' . $record->drlist;
              print "</span></a></li>\n";
            }
          }

=>

foreach ($set as $DATA => $record) {
              print "<li>\n";
              print '<a href="photos/'. $record->e_link . '/' . $record->date . '">' . $record->about . '<br /><span>' . $record->drlist;
              print "</span></a></li>\n";
      
          }
1 Like

Much bigger update :grin:

And thanks - I’ll give refactoring a try and report back shortly on the result.

Cheers, Barry

I’ve certainly discovered some further issues :grimacing:
Though for now, this is working good @m_hutley and thanks for the code examples.

Cool, I’ll do further testing and work towards a bigger refactoring once I get a better understanding of the main issues.

Cheers for the help :sunglasses:

1 Like