1

I am creating a small project for school. I've been able to create a function which completes a working query(tested using SQL).

The error I'm getting is; Notice: Trying to get property 'tournamentName' of non-object in E:\programs\XAMPP\htdocs\Esports\View\Tournament.php on line 34.

but I'm getting that for all of them? not sure why.

If anyone could help, I would be grateful.

My code is below.

my DataAccess.php Function that runs the query is;

function getAllTourament()
{
    global $pdo; 
    $statement = $pdo ->prepare (' SELECT tournament.tournamentName,
    tournament.console,
    tournament.no_of_players,
    tournament.prize,
    game.gName,
    sponsors.sponsorName,
    venue.venue_name,
    venue.venue_location,
    venue.venue_date,
    venue.venue_time
FROM tournament, game, sponsors, venue
    WHERE tournament.gameID = game.gameID AND tournament.sponsorID = sponsors.sponsorID AND tournament.venueID = venue.venueID
    ORDER by tournament.tournamentName, console, no_of_players, prize , gName , sponsorName , venue_name,venue_location, venue_date, venue_time');
    $statement->execute();
     $result = $statement;
    return $result;   
}

Controller

    <?php

/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

require_once ("../Model/dataAccess.php");
require_once ("../Model/Game.php");
require_once ("../Model/Tournament.php");
require_once ("../Model/Sponsor.php");
require_once ("../Model/Venue.php");


$allTournament = getAllTourament();
//$allTournament = getAllT(); 

View Tournament.php

  <?php
require_once ("../Controller/allTournament.php");

?>

<?php
require "header.php";
?>

<main>
     <table>
                <thead>
                    <tr>
                        <th>Tournament Name</th>
                        <th>Console</th>
                        <th>No Of Players</th>
                        <th>Prize</th>
                        <th>Game Name </th>
                        <th>Sponsor Name </th>
                        <th>Venue Name </th>
                        <th>Venue Location </th>
                        <th>Venue Date </th>
                        <th>Venue time </th>
                    </tr>
                </thead>
                <tbody>
                    <tr> <?php foreach ($allTournament as $Tview): ?>
                        <td><?= $Tview-> tournamentName ?> </td> 
                        <td><?= $Tview-> console ?> </td>
                        <td><?= $Tview-> no_of_players ?> </td> 
                        <td><?= $Tview-> prize ?> </td> 
                        <td><?= $Tview-> gName ?> </td>
                        <td><?= $Tview-> sponsorName ?> </td>
                        <td><?= $Tview-> venue_name ?> </td>
                        <td><?= $Tview-> venue_location ?> </td>
                        <td><?= $Tview-> venue_date ?> </td>
                        <td><?= $Tview-> venue_time ?> </td>

                    </tr><?php endforeach ?>

                </tbody>

            </table>
</main>

<?php
require "footer.php";
?>

Model Tournament.php

class Tournament {
   // private $gameId; 
    private $tournamentName;
    private $console;
    private $no_of_players;
    private $prize;



    /* magic getter and setter */

    function __get($name) 
    {
        return $this->$name;
    }

    function __set ($name, $value)
    {
        $this->$name = $value;
    }



}

Model Game.php

class Game {
   // private $gameId; 
    private $gName;
    private $rating;
    private $genre;
    private $price;
    private $date;


    /* magic getter and setter */

    function __get($name) 
    {
        return $this->$name;
    }

    function __set ($name, $value)
    {
        $this->$name = $value;
    }  
}

Model Venue.php

class Venue {
   // private $id; 
    private $venue_name;
    private $venue_location;
    private $venue_time;
    private $venue_date;
    private $venue_seats;



    /* magic getter and setter */

    function __get($name) 
    {
        return $this->$name;
    }

    function __set ($name, $value)
    {
        $this->$name = $value;
    }
}

Model Sponsor.php

class Sponsor 
{
    private $sponsorName;
    private $sponsorWsite;       
    private $sponsorType;
    private $sponsorLength;
                function __get($name) {
        return $this->$name;
    }

    function __set($name, $value) {
        $this->$name = $value;
    }



}
Bhavik
  • 13
  • 5
  • Why is there an space after arrow (`->`)? It should be like this: `$Tview->tournamentName`. Remove it and try again – Amir Mar 11 '19 at 19:31
  • 1
    @Amir How would a space be important? – Dharman Mar 11 '19 at 19:32
  • A word of advice. Use JOINs in SQL. The SQL is easier to read and it is the recommended way of joining tables. You could then make it simpler by using `USING()` instead of `ON` – Dharman Mar 11 '19 at 19:35
  • @Amir Thank you for replying, I've tried taking out space's and it still doesn't work. – Bhavik Mar 11 '19 at 19:35
  • Could you `var_dump($allTournament);` before the loop? – Dharman Mar 11 '19 at 19:36
  • @Dharman why wouldn't it be?! It is a syntax error. Besides, I just tested it and got the exact error – Amir Mar 11 '19 at 19:38
  • @Bhavik Are you sure you removed all the spaces after all the `->`? I just tested this (with the space after arrow) and got the exact same error. And fixed it with removing the space – Amir Mar 11 '19 at 19:40
  • @Amir Maybe I misunderstood your comment. I tried it like this: https://ideone.com/hYgfut – Dharman Mar 11 '19 at 19:42
  • @Dharman This is what i got; object(PDOStatement)#2 (1) { ["queryString"]=> string(553) " SELECT tournament.tournamentName, tournament.console, tournament.no_of_players, tournament.prize, game.gName, sponsors.sponsorName, venue.venue_name, venue.venue_location, venue.venue_date, venue.venue_time FROM tournament, game, sponsors, venue WHERE tournament.gameID = game.gameID AND tournament.sponsorID = sponsors.sponsorID AND tournament.venueID = venue.venueID ORDER by tournament.tournamentName, console, no_of_players, prize, gName , sponsorName , venue_name,venue_location, venue_date, venue_time" } – Bhavik Mar 11 '19 at 19:43
  • 1
    Access your fields using this syntax: `$Tview['tournamentName']` – Amir Mar 11 '19 at 19:44
  • @Amir OMG Thank you, that worked – Bhavik Mar 11 '19 at 19:50
  • I'm glad it worked. So I'll post the answer – Amir Mar 11 '19 at 19:52
  • @Dharman you was talking about Joins, how would that work for my code – Bhavik Mar 11 '19 at 19:53
  • @Amir Please do post the answer :) again Thank you, ive spent hours trying to get it to work. – Bhavik Mar 11 '19 at 19:54
  • https://mariadb.com/kb/en/library/comma-vs-join/ – Dharman Mar 11 '19 at 19:54
  • Is it weird to anyone else when someone loops on the PDOStatment object ... see `$result = $statement;` They probably mean to do a fetch here. I don't think I have ever just looped on it, not sure why. Just force of habit I guess. – ArtisticPhoenix Mar 11 '19 at 20:05
  • @ArtisticPhoenix It does make things much less readable, at least in this case. I loop like this sometimes, but usually I get so little data that I just `fetchAll` and return the whole result set. – Dharman Mar 11 '19 at 20:09
  • @Dharman - there is not much difference in performance even between that and fetch. I work with large data sets up to 120 milliion rows, which I had to do some stuff with unbuffered queries. Both fetch and fetch all use buffered queries to minimize network tragic, so the cost for each is about the same. I just prefer the flexibility of the fetch modes when pulling data so I'm just so used to using fetch etc. that It always seems like an error. I have also used mysqli, and even mysql back in 2008-2009. Nither of which you could use foreach on. – ArtisticPhoenix Mar 12 '19 at 00:31

1 Answers1

0

Access your fields using this syntax:

$Tview['tournamentName']

Notice: Trying to get property of non-object error

Happens when you try to access a property of an object while there is no object.

A typical example for a non-object notice would be

$users = json_decode('[{"name": "hakre"}]');
echo $users->name; # Notice: Trying to get property of non-object

In this case, $users is an array (so not an object) and it does not have any properties.

This is similar to accessing a non-existing index or key of an array (see Notice: Undefined Index).

This example is much simplified. Most often such a notice signals an unchecked return value, e.g. when a library returns NULL if an object does not exists or just an unexpected non-object value (e.g. in an Xpath result, JSON structures with unexpected format, XML with unexpected format etc.) but the code does not check for such a condition.

As those non-objects are often processed further on, often a fatal-error happens next on calling an object method on a non-object (see: Fatal error: Call to a member function ... on a non-object) halting the script.

It can be easily prevented by checking for error conditions and/or that a variable matches an expectation. Here such a notice with a DOMXPath example:

$result  = $xpath->query("//*[@id='detail-sections']/div[1]");
$divText = $result->item(0)->nodeValue; # Notice: Trying to get property of non-object

The problem is accessing the nodeValue property (field) of the first item while it has not been checked if it exists or not in the $result collection. Instead it pays to make the code more explicit by assigning variables to the objects the code operates on:

$result  = $xpath->query("//*[@id='detail-sections']/div[1]");
$div     = $result->item(0);
$divText = "-/-";
if (is_object($div)) {
    $divText = $div->nodeValue;
}
echo $divText;

Related errors:

Amir
  • 1,885
  • 3
  • 19
  • 36