Someone please help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
hunterhp
Forum Commoner
Posts: 46
Joined: Sat Jan 22, 2005 5:20 pm
Contact:

Someone please help

Post by hunterhp »

I don't understand the error I'm getting, and why I'm getting it.

This code right here works perfectlly fine

This is tournament.class

Code: Select all

function AddTournament() {

         $title = $_POSTї'name'];
         $game = $_POSTї'game'];
         
            
               $tourney = $DB->Query("INSERT INTO tournaments VALUES ('', '$title', '$game', '', '')");
               
               if ($tourney) {
               
                  echo "Tournament has been added to the database";
                  
               }
               else
               {
               
                  echo "Tournament could not be added ". mysql_error();
               }
}
However, this code gives me an error

Code: Select all

function ViewTournaments() {

         if (isset($_GETїid])) {
         
            $id = $_GETїid];
         
    //Line 40 ->        $tourneys = $DB->Query("SELECT * FROM tournaments WHERE id='$id'");
            $row = mysql_fetch_row($tourneys);
            
            if ($row) {
            
            $this->tournamentї'id'] = $rowї0];
            $this->tournamentї'name'] = $rowї1];
            $this->tournamentї'game'] = $rowї2];
            $this->tournamentї'standings'] = $rowї3];
            $this->tournamentї'winners'] = $rowї4];
            
            }
            else
            {
            
            echo "Query unsuccessful on line ". __LINE__ ." in ".__FILE__;
            
            }
            
         }
         else
         {
         
      //Line 63 ->      $tourneys = $DB->Query("SELECT * FROM tournaments ORDER BY id DESC");
         
            if ($tourneys) {
            while ($row = mysql_fetch_row($tourneys)) {
         
                  $this->tournamentї'id'] = $rowї0];
                  $this->tournamentї'name'] = $rowї1];
            
            }
            }
            else
            {
            
            echo "Query unsuccesful on line ". __LINE__ ." in ". __FILE__;
            
            }
         }
         
         return;

}
It gives me a "
Fatal error: Call to a member function on a non-object in /home/www/hunterhp.freeownhost.com/tournament.class on line 40" (If $_GET[id] is set) or it gives me a "
Fatal error: Call to a member function on a non-object in /home/www/hunterhp.freeownhost.com/tournament.class on line 63" if it's not.

I don't understand why this is happening. I used $DB->Query on the function above and it works perfectly, but not on ViewTournaments()

The tournament.class gets outputed on tournament.php, which I'll show the source here.

tournament.php

Code: Select all

<?php
// Tournament Script tournaments.php Created January 31, 2005 at 3:00 A.M.
include("config.inc");

include("tournament.class");

?>
<html>
<head>
<title>Tournament</title>
</head>
<body>
<?
$Render = new Tournament;

$Render->Template('tournament.html');

if (isset($_POST&#1111;submit])) &#123;

$Render->AddTournament();
&#125;
$Render->ViewTournaments();

$Render->CreatePage();

?>

<form action='tournament.php' method='post'>
Title: <input type='text' name='name'><br>
Game: <input type='text' name='game'><br>
<input type='submit' name='submit' value='Submit'>
</body>
</html>
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

1-) You are putting data in your query without validating that data.
2-) You are using global variables in a function (usually a bad code smell).
3-) talking about global variables, $DB is an object that doesn't exits (at least not in the AddTournament and ViewTournaments functions.. So pass it as a parameter...
hunterhp
Forum Commoner
Posts: 46
Joined: Sat Jan 22, 2005 5:20 pm
Contact:

Post by hunterhp »

$DB is set on config.inc page.

As such.

$DB = new SQL; // From db.class

How come it worked for AddTournament though?
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

$DB is set in config.inc -> $DB is a global variable....


in a function, $DB does not exist.

Code: Select all

$db = "foo";

function bar()
&#123;
    echo "bar: $db <br>";
&#125;

function bar2($db)
&#123;
   echo "bar2: $db <br>";
&#125;

function bar3()
&#123;
    global $db;
    echo "bar3: $db <br>";
&#125;

bar();
bar2($db);
bar3();
hunterhp
Forum Commoner
Posts: 46
Joined: Sat Jan 22, 2005 5:20 pm
Contact:

Post by hunterhp »

But I thought OOP was different?

And how come it works for inserting values to the database(IE: AddTournaments).

Here's the db.class one.

Code: Select all

<?php
// Tourney Script, Started on January 31, 2005 at 1:00 A.M.

class SQL&#123;

var $dbc;
var $result;

    function Connect() &#123;
    global $host, $user, $pw, $database;
    
        $this->dbc = mysql_connect($host, $user, $pw);
        
        if ($this->dbc)
        &#123;
            mysql_select_db($database, $this->dbc);
        &#125;
        else
        &#123;
            die('Cannot connect to database: ' . mysql_error());
        &#125;
    &#125;
    
    function Query($query)&#123;
    
    $this->result = mysql_query($query, $this->dbc);
                  if ($this->result)
                  &#123;
    
                     return $this->result;
       
                   &#125;
                   else &#123;
    
                     die('Cannot Query database: ' . mysql_error());
         
                   &#125;
       
    
    &#125;
    
    function Close($dbc) &#123;
    
    if ($close = mysql_close($dbc)) &#123;
    
       return TRUE;
       
       &#125;
    return $close;
    &#125;
    
&#125;
?>
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

From http://www.php.net/oop
A class is a collection of variables and functions working with these variables.

So in all the class methods/functions you have a form of variables, class members. These can be rerenced like $this->name_of_variable.

Class instances are just variables. So if you want to use them in a function, you should pass them (or a reference) as a parameter or declare them global.
hunterhp
Forum Commoner
Posts: 46
Joined: Sat Jan 22, 2005 5:20 pm
Contact:

Post by hunterhp »

I still don't understand why AddTournament worked, and ViewTournament didn't.

What should I do different you think? Change my entire code?
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

i would probably write it like:

Code: Select all

class tournament
&#123;
     var $DB; // connection to the database

       // php4 constructor
      function tournament()
      &#123;
           $this->DB = new SQL;
      &#125;

      // add a tournament
     function addTournament($name, $title)
     &#123;
           // make sure the data is ready to be in a query
          $name = $this->DB->sanitize($name);
          $title = $this->DB->sanitize($title);       

          $query = "INSERT INTO foo VALUES ('', '$title', '$name', ...)";
          $result = $this->DB->query($query);

         ...
    &#125;

    // view tournaments would be similar....

&#125;
Post Reply