Solved

Accesing class from another class

Posted on 2011-09-20
4
314 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 110

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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

632 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