Jump to content

Recommended Posts

Hi Guys,

 

I only get this Warning when I use it in a class.

 

This is the class script:


        require_once("_class/delete.php");
        $delete = new delete($ID);

class database
{
	function __construct(){
		$this->connect();
	}

	public function connect(){
	   mysql_connect("localhost","**********","**********")or die("<h1>Connect ERROR</h1>".mysql_error());
		mysql_select_db("**********")or die("<h1>MYSQL SELECT ERROR</h1>".mysql_error());
	}

	public function query($sql)
	{
		return $query = mysql_query($sql)or die("<h1> MYSQL QUERY ERROR </h1>".mysql_error());
	}

	public function fetch($query)
	{
		return $fetch = mysql_fetch_assoc($query)
	}

}

class delete extends database
{
	function __construct($ID)
	{
		$sql = "SELECT path FROM news WHERE ID='$ID' LIMIT 1";
		$query = $this->query($sql);
		$fetch = $this->fetch($query);
		$this->delImage($fetch['path']);
		exit();
	}

	function delImage($image)
	{
		unlink($image);	
	}
}

 

This is the not class working code

 


$sql = "SELECT path FROM news WHERE ID='$ID' LIMIT 1";
					$query = mysql_query($sql)or die("<h1> MYSQL QUERY ERROR </h1>".mysql_error());
					$fetch = mysql_fetch_assoc($query);

					$image = $fetch['path'];

					unlink($image);

 

Please could someone tell me it happens in a class.

 

Thanks for your help,

 

tecmeister.

 

Link to comment
https://forums.phpfreaks.com/topic/204239-class-warning-mysql_fetch_assoc/
Share on other sites

Note: Parent constructors are not called implicitly if the child class defines a constructor. In order to run a parent constructor, a call to parent::__construct() within the child constructor is required.

 

And you should be developing and debugging php code on a system with error_reporting set to E_ALL and display_errors set to ON so that php will both report and display all the errors it detects. You would have seen that the mysql connection was not being made.

Even if I remove the __constructor and have it as a basic method I still get the same Warning.

 

I have got the:

    error_reporting(-1);

    ini_set('display_error', 1);

 

The thing that I dont understand it that it has know problem not in a class but when in a class it gives me th same Warning.  I have the __constructor connecting to the database at the start of the page.

 

Much appreciated for your help,

 

tecmeister.


require_once("_class/delete.php");
        $delete = new delete($ID);

   class database
   {
      function __construct(){
         $this->connect();
      }
      
      public function connect(){
         mysql_connect("localhost","**********","**********")or die("<h1>Connect ERROR</h1>".mysql_error());
         mysql_select_db("**********")or die("<h1>MYSQL SELECT ERROR</h1>".mysql_error());
      }
      
      public function query($sql)
      {
         return $query = mysql_query($sql)or die("<h1> MYSQL QUERY ERROR </h1>".mysql_error());
      }
      
      public function fetch($query)
      {
         return $fetch = mysql_fetch_assoc($query)
      }
      
   }


class delete extends database
{
	function selectDB($ID)
	{	
		$sql = "SELECT path FROM news WHERE ID='$ID'";
		$query = $this->query($sql);
		$fetch = $this->fetch($query);
		$this->delImage($fetch['path']);
		exit();
	}

	function delImage($image)
	{
		unlink($image);	
	}
}

 

I have put the exit() in so I can see the Warning.

This is the database class that I use to connect to the database.

On the index page I refer to the database class.

 


class database
{
	function __construct(){
		$this->connect();
	}

	public function connect(){
	   mysql_connect("localhost","nurevol1_teci","b748a9766483850584695e364c8b2d34")or die("<h1>Connect ERROR</h1>".mysql_error());
		mysql_select_db("nurevol1_pyung")or die("<h1>MYSQL SELECT ERROR</h1>".mysql_error());
	}

	public function query($sql)
	{
		return $query = mysql_query($sql)or die("<h1> MYSQL QUERY ERROR </h1>".mysql_error());
	}

	public function fetch($query)
	{
		return $fetch = mysql_fetch_assoc($query);
	}

}

 

This is the class that I'm using to delete te Image.

 


class delete extends database
{
	function selectDB($ID)
	{	
		$sql = "SELECT path FROM news WHERE ID='$ID'";
		$query = parent::query($sql);
		$fetch = parent::fetch($query);
		$this->delImage($fetch['path']);
		exit();
	}

	function delImage($image)
	{
		unlink($image);	
	}
}

 

This is the code that I use to get the get the class and refer to it.

 


// GETTING THE ID FROM THE URL
$ID = $_GET['id'];
$page = $_GET['delete'];

if((isset($_GET['delete'])) && ($_GET['delete'] == 'news'))
{

require_once("_class/delete.php");
$delete = new delete();

$delete->selectDB($ID);

}

 

O and just to note that I have just put the parent:: into try new stuff.  I was using $this->

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.