Solved

Accesing class from another class

Posted on 2011-09-20
4
307 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 108

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
jQuery detect if it is a mobile device 3 95
Animated .jpg? 13 57
Site hacked - decoding the PHP? 15 57
PHPStorm debugging issues 1 21
Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
The viewer will learn how to count occurrences of each item in an array.
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…

919 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now