Solved

Accesing class from another class

Posted on 2011-09-20
4
309 Views
Last Modified: 2012-08-13
Hi experts, I have the following php:
<?php
//require_once('classPark.php');
require_once('DBConn.php') ;
require_once('classClub.php');
?>
       $team=new Team($_POST['TeamId']);
       $team->Details();

Open in new window


and two classes DBConn
class DBConn
      {
        var $db_connection = null;        // Database connection string
        var $db_server = null;            // Database server
        var $db_database = null;          // The database being connected to
        var $db_username = null;          // The database username
        var $db_password = null;          // The database password
        var $CONNECTED = false;           // Determines if connection is established

        /** NewConnection Method
         * This method establishes a new connection to the database. */
        public function NewConnection() //$server, $database, $username, $password) eliminated , just std connection
        {
            // Assign variables
            global $db_connection, $db_server, $db_database, $db_username, $db_password;
            $db_server = "xxxxxx";
            $db_database = "xxxxxx";
            $db_username = "xxxxxx";
            $db_password = "xxxxx";
            echo "Entro a newconnection";
            // Attempt connection
            try
            {
                // Create connection to MYSQL database
                // Fourth true parameter will allow for multiple connections to be made
                $db_connection = mysqli_connect ("xxxxx", "xxxxx", "xxxxxxx","xxxx");
               // mysqli_select_db ($db_connection,$database);
                if (!$db_connection)
                {
                    throw new Exception('MySQL Connection Database Error: ' . mysqli_error());
                }
                else
                {    echo "Se Conecto";
                    $CONNECTED = true;
                }
            }
            catch (Exception $e)
            {
                echo $e->getMessage();
            }
        }
 

Open in new window


and class Team

class Team extends Club{
        var $idTeam = null;        // Database connection string
        var $teamName = null;            // Database server
        var $age = null;          // The database being connected to
        var $code = null;          // The database username
        var $coach = null;          // The database password
        var $coachMail = false;
        var $baseField = false;           // Determines if connection is established

      public function __construct($teamid) {
            $dbc = new DBConn(); $dbc -> NewConnection();
            echo isset($CONNECTED)? "Connected si":"Connected no";
            echo isset($db_connected)? "dbConnected si":"dbConnected no";
            $query = "select * from analysis_Futbol.Teams where idTeams=".$teamid;
            $result = mysqli_query($db_connection, $query)  ;
            while ($row = mysqli_fetch_array($result)){
                  $idTeam = $teamid;
                  $teamName = $row['TeamName'];
                  $age = $row['Age']      ;
                  $code = $row['Code']    ;
                  $baseField = $row['BaseField'];
                  $coach =$row['Coach']  ;
                  $coachMail = $row['CoachMail'];
            }
            $dbc->close();
      }
      static function dUse() {
        Self::Show();
        Parent::Show();
      }
      static function Show() {
         echo __FILE__.'('.__LINE__.'):'.__METHOD__."\n";
      }
      public function Details(){
            $details  =  "<p class=\"teamdetails\">";
            $details .=  $age."<br />".$teamName."<br />";
            $details .=  "FYSA Code: ".$code."<br />";
            $details .=  "Base Field: ".$baseField." <br />";
            $details .=  "Coach : ".$coach."<br />( ".$coachMail." )<br />";
            $details .=  "</p>";
            return $details;
      }
}

Open in new window



I am getting the following error:
Warning: mysqli_query() expects parameter 1 to be mysqli, null given in /home1/analysis/public_html/d4usoft/FieldManager/classClub.php on line 24

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in /home1/analysis/public_html/d4usoft/FieldManager/classClub.php on line 26

Can anyone help ,me understand why it is happening and how to solve it.
Sorry, guys new to oop and trying to learn.
Thank You
0
Comment
Question by:howtodoit
4 Comments
 
LVL 11

Expert Comment

by:AlexanderR
ID: 36571634
Will have to see classClub.php. Thanks.
0
 
LVL 1

Expert Comment

by:niepodoba
ID: 36572791
AlexanderB is right it'd help.

But anyway. This means your connection to the database isn't made and you're trying to perform some operation on it.

1. check your link name for typos.
2. try selecting database after connecting if you don't do it.
3. if you don't want to share classClub.php at least show us line 24 and line where you make connection to the database.
0
 
LVL 4

Accepted Solution

by:
Fugas earned 500 total points
ID: 36579194
replace $result = mysqli_query($db_connection, $query)  ; with
$result = mysqli_query($dbc->db_connection, $query)  ;
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 36595273
See http://php.net/manual/en/language.variables.scope.php

Line 12 in class Team:

echo isset($CONNECTED)? "Connected si":"Connected no";

$CONNECTED is an undefined variable in the scope of the class constructor.

Line 13 in class Team:

echo isset($db_connected)? "dbConnected si":"dbConnected no";

$db_connected is an undefined variable in the scope of the class constructor.  Also there appears to be some confusion about the name (maybe).  In some places it seems to be $db_connection.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question