Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 321
  • Last Modified:

Accesing class from another class

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
howtodoit
Asked:
howtodoit
1 Solution
 
AlexanderRCommented:
Will have to see classClub.php. Thanks.
0
 
niepodobaCommented:
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
 
FugasCommented:
replace $result = mysqli_query($db_connection, $query)  ; with
$result = mysqli_query($dbc->db_connection, $query)  ;
0
 
Ray PaseurCommented:
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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now