play_ Posted October 18, 2008 Share Posted October 18, 2008 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 ); Quote Link to comment Share on other sites More sharing options...
xtopolis Posted October 18, 2008 Share Posted October 18, 2008 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. Quote Link to comment Share on other sites More sharing options...
play_ Posted October 18, 2008 Author Share Posted October 18, 2008 that still didn't work =/ i'll try more again tomorrow. thank you. Quote Link to comment Share on other sites More sharing options...
xtopolis Posted October 18, 2008 Share Posted October 18, 2008 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(); ?> Quote Link to comment Share on other sites More sharing options...
play_ Posted October 18, 2008 Author Share Posted October 18, 2008 Alright i lied. your solution did work. thanks very much xtopolis =) Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.