Jump to content

[SOLVED] Aggregation issues


play_

Recommended Posts

Having some issues here.

 

I create an SQL object and pass it as a parameter so i can use it inside another class definition (Pages). The query runs, but if i try to invoke any other method of the SQL object, it doesn't work. I think it's the way data is being passed around the SQL class. any thoughts?

 

class Pages {

function __construct( $sql ) {
	$this->sql = (object) $sql;
}

function add( $name, $title, $content, $show_link, $is_default ) {

                $query = "select * from pages";
                $this->sql->query( $query );

	while($r = $this->sql->fetchArray() ) {
                       // returns blank!
		echo $r['id'];
	}
}
}

 

 

 

 

<?php
class SQL {

function __construct() {
	$this->host		= 'secret';
	$this->user		= 'secret';
	$this->password = 'secret';
	$this->database = 'secret';

	$this->result;
	$this->querycount;
	$this->connect();
	$this->select();
}

function connect() {
	$this->linkid = @ mysql_connect($this->host, $this->user, $this->password);
	if(!$this->linkid) {
		echo "--[ could not connect to sql database ]--"; 	exit();
	}
}

function select() {
	if(!@ mysql_select_db($this->database, $this->linkid)) {
		echo "--[ could not select database ]--";	exit();
	}
}


function query($query) {
	if( @ mysql_query($query, $this->linkid) ) {
		$this->querycount++;
		return true;
	} else {
		echo "<b>Error:</b>" . mysql_error($this->linkid);				
		return false;
	}	
}


function fetchArray() {
	$row = @ mysql_fetch_array($this->result);
	return $row;
}



}

 

 

 

Usage example: test.php

$sql	= new SQL();
$page = new Pages( $sql );

Link to comment
Share on other sites

You didn't assign $this->result to anything

 

I think you meant to add it in the query function, but forgot:

 

Change to:

<?php
...
  function query($query) {
    if( $this->result = @mysql_query($query, $this->linkid) ) {

..
}
?>

 

I added that, and then did a $page->add(); on test.php, and it showed me the ids of my test table.

Link to comment
Share on other sites

Well, the issue could be with your add function, all those parameters.  However this code works for me, removing the parameters from add.

 

Also, FYI, I don't know what editor you're using, but copy/pasting your code creates a lot of extra vertical, and horizontal spacing on my editor(Notepad2) in windows @_@

 

pages.php

<?php
class Pages {

  function __construct( $sql ) {
    $this->sql = (object) $sql;
  }//__c

  function add( $name, $title, $content, $show_link, $is_default ) {
    $query = "select * from pages";
    $this->sql->query( $query );

    while($r = $this->sql->fetchArray() ) {
      echo $r['id'];// returns blank!
    }
  }//add

}//class
?>

 

sql.php

<?php
class SQL {

  function __construct() {
    $this->host= 'secret';
    $this->user= 'secret';
    $this->password = 'secret';
    $this->database = 'secret';
    $this->result = null;
    $this->querycount = 0;

    $this->connect();
    $this->select();
  }//__c

  function connect() {
    $this->linkid = @ mysql_connect($this->host, $this->user, $this->password);

    if(!$this->linkid) {
      echo "--[ could not connect to sql database ]--"; 
      exit();
    }
  }//connect

  function select() {
    if(!@ mysql_select_db($this->database, $this->linkid)) {
      echo "--[ could not select database ]--";
      exit();
    }
  }//select

  function query($query) {
    if($this->result = @ mysql_query($query, $this->linkid) ) {
      $this->querycount++;
      
        return true;
    } else {
      echo "<b>Error:</b>" . mysql_error($this->linkid);
        return false;
    }
  }//query

  function fetchArray() {
    $row = @ mysql_fetch_array($this->result);
      return $row;
  }

}//class
?>

 

test.php

<?php
  $sql= new SQL();
  $page = new Pages( $sql );
  $page->add();
?>

Link to comment
Share on other sites

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.