Solved

Accesing class from another class

Posted on 2011-09-20
4
306 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
Comment Utility
Will have to see classClub.php. Thanks.
0
 
LVL 1

Expert Comment

by:niepodoba
Comment Utility
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
Comment Utility
replace $result = mysqli_query($db_connection, $query)  ; with
$result = mysqli_query($dbc->db_connection, $query)  ;
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
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…
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 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…

763 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

13 Experts available now in Live!

Get 1:1 Help Now