Jump to content

calling a class within a class


tombruton

Recommended Posts

I have a class in which I have a function called connection. I am now trying to call this function from another class, but it will not work. It works if I put the code in from the other function rather than calling it but that defeats the purpous.

 

class locationbox

 

{

function location()

{

 

 

$databaseconnect = new databaseconnect();

$databaseconnect -> connection();{

 

$result = mysql_query("SELECT * FROM locations");

 

while($row = mysql_fetch_array($result)) // line that now gets the error, mysql_fetch_array() expects parameter 1 to be resource, boolean given //in

  {

  echo "<option>" . $row['location'] . "</option>";

    }

 

}

}}

Link to comment
https://forums.phpfreaks.com/topic/248356-calling-a-class-within-a-class/
Share on other sites

This should work.

 

class locationbox
{
    public $dbconnect;
    
    public function __construct()
    {
        $this -> $dbconnect = new databaseconnect();
        $dbconnect -> connection();
    }
    
    public function location()
    {
        $result = mysql_query("SELECT * FROM locations");

        while ($row = mysql_fetch_array($result)) // line that now gets the error, mysql_fetch_array() expects parameter 1 to be resource, boolean given //in
        {
            echo "<option>" . $row['locations'] . "</option>";
        }
    }
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.