metalblend Posted January 31, 2003 Share Posted January 31, 2003 I\'ve been messing around with objects and classes lately and can\'t figure out why this wont work. I have index.php which uses the pbMySQL class (pbMySQL.class.php) and methods: include("lib/pbMySQL.class.php"); $db = new pbMySQL; $db->pbMySQL_open("localhost","user","pass"); $db->pbMySQL_useDB("database"); // $q=$db->pbMySQL_query("SELECT * FROM pbn_news ORDER BY id DESC"); $q=mysql_query("SELECT * FROM pbn_news ORDER BY id DESC"); if(isset($q)) { while($row=mysql_fetch_array($q)) { print $row[\'name\']."<br>rn"; } } else { print "query returned FALSE"; } $db->pbMySQL_close(); My problem is that when I use the first query method (which is commented; uses the pbMySQL_query() method) I do not get the mysql result returned, but if I use the same query without the method, I get the result. Here is the method from my class: function pbMySQL_query($query) { if(!isset($this->conn) || !isset($query)) { print "Error : argument(s) missing.<br>rn"; } else { $this->query = mysql_query($query,$this->conn) or die("Error : query failed.<br>rnmysql said: <i>".mysql_error()."</i><br>rn"); if(isset($this->query)) { return $this->query; } else { return FALSE; } } } The method returns FALSE and I don\'t understand why. Is it something in particular I have to do different when using a class? Any help is appreciated.. Thanks. Link to comment https://forums.phpfreaks.com/topic/112-mysql-result-cant-be-returned-from-class/ Share on other sites More sharing options...
metalblend Posted January 31, 2003 Author Share Posted January 31, 2003 nevermind.. got it. When I used $query as a parameter, it conflicted with $this->query Link to comment https://forums.phpfreaks.com/topic/112-mysql-result-cant-be-returned-from-class/#findComment-332 Share on other sites More sharing options...
pallevillesen Posted January 31, 2003 Share Posted January 31, 2003 EDIT: Aaaargghhh. too late.... Please post your working code just for friday fun... P. /EDIT You\'re close... I\'m not good at OOP but this works: include("lib/pbMySQL.class.php"); $db = new pbMySQL; $db->pbMySQL_open("localhost","user","pass"); $db->pbMySQL_useDB("database"); $db->pbMySQL_query("SELECT * FROM pbn_news ORDER BY id DESC"); while($row=mysql_fetch_array($db->query)) { print $row[\'name\']."<br>rn"; } $db->pbMySQL_close(); function pbMySQL_query($query) { if(!isset($this->conn) || !isset($query)) { print "Error : argument(s) missing.<br>rn"; } else { $this->query = mysql_query($query,$this->conn) or die("Error : query failed.<br>rnmysql said: <i>".mysql_error()."</i><br>rn"); } } I changed a few bits in both parts... Link to comment https://forums.phpfreaks.com/topic/112-mysql-result-cant-be-returned-from-class/#findComment-333 Share on other sites More sharing options...
metalblend Posted January 31, 2003 Author Share Posted January 31, 2003 Aha! I knew there was a better way than using: $q=$db->pbMySQL_query(); Thanks again Palle. Link to comment https://forums.phpfreaks.com/topic/112-mysql-result-cant-be-returned-from-class/#findComment-335 Share on other sites More sharing options...
metalblend Posted January 31, 2003 Author Share Posted January 31, 2003 Here\'s what I got to work.. for Friday fun. function pbMySQL_query($str) { if(!isset($this->conn) || !isset($str)) { print "Error : argument(s) missing.<br>rn"; } else { $this->query = mysql_query($str,$this->conn) or die("Error : query failed.<br>rnmysql said: <i>".mysql_error()."</i><br>rn"); } } $db->pbMySQL_query("SELECT * FROM pbn_news ORDER BY id DESC"); if(isset($db->query)) { while($row=mysql_fetch_array($db->query)) { print "<div align=\'left\'>rn<div align=\'left\' style=\'background-color: #E0E0E0; padding: 2px;\'>rn".$row[\'name\']."<br>rn</div>rn".$row[\'message\']."<br><br></div>rn"; } } Link to comment https://forums.phpfreaks.com/topic/112-mysql-result-cant-be-returned-from-class/#findComment-337 Share on other sites More sharing options...
pallevillesen Posted January 31, 2003 Share Posted January 31, 2003 Nice.... Here\'s my sql class definition: So I use something like: $sql->query("secret bioinformatics content... "); for ($i = 0; $i < $sql->rows; $i++) { $sql->Fetch($i); printf("<tr> <td><A href=\'getpromotor.php?stable_id=%s\' target=\'_promotorview\'>Get promotor</a></td> <td><A href=\'http://www.ensembl.org/Homo_sapiens/geneview?gene=%s\' target=\'_geneview\'>%s</a></td> <td>%s</td> <td>%s</td> <td>%s</td> </tr>",$sql->data[0], $sql->data[0], $sql->data[0], $sql->data[1], $sql->data[2], $sql->data[3]); } mysqlbase.inc.php: P. <? /* * Utility routines for MySQL. */ class MySQL_class { var $db, $id, $result, $rows, $data, $a_rows; var $user, $pass, $host, $errorno; /* Setup selects host, username and password */ function ServerConnect ($host, $user, $pass) { $this->host= $host; $this->user = $user; $this->pass = $pass; } /* Create select database to work on and connects to it */ function Create ($db) { $this->db = $db; $this->id = @mysql_pconnect($this->host, $this->user, $this->pass) or MySQL_ErrorMsg("Unable to connect to MySQL server: $this->host : \'$SERVER_NAME\'"); # Call select database function, since we found the server $this->SelectDB($db); } function SelectDB ($db) { @mysql_select_db($db, $this->id) or MySQL_ErrorMsg ("Unable to select database: $db"); } # Use this function is the query will return multiple rows. Use the Fetch # routine to loop through those rows. function Query ($query) { $this->result = @mysql_query($query, $this->id) or MySQL_ErrorMsg ("Unable to perform query: $query"); $this->rows = @mysql_num_rows($this->result); $this->a_rows = @mysql_affected_rows($this->id); } # Use this function if the query will only return a # single data element. function QueryItem ($query) { $this->result = @mysql_query($query, $this->id) or MySQL_ErrorMsg ("Unable to perform query: $query"); $this->rows = @mysql_num_rows($this->result); $this->a_rows = @mysql_affected_rows($this->id); $this->data = @mysql_fetch_array($this->result) or MySQL_ErrorMsg ("Unable to fetch data from query: $query"); return($this->data[0]); } # This function is useful if the query will only return a # single row. function QueryRow ($query) { $this->result = @mysql_query($query, $this->id) or MySQL_ErrorMsg ("Unable to perform query: $query"); $this->rows = @mysql_num_rows($this->result); $this->a_rows = @mysql_affected_rows($this->id); $this->data = @mysql_fetch_array($this->result) or MySQL_ErrorMsg ("Unable to fetch data from query: $query"); return($this->data); } function Fetch ($row) { @mysql_data_seek($this->result, $row) or MySQL_ErrorMsg ("Unable to seek data row: $row"); $this->data = @mysql_fetch_array($this->result) or MySQL_ErrorMsg ("Unable to fetch row: $row"); } function Insert ($query) { $this->result = @mysql_query($query, $this->id) or MySQL_ErrorMsg ("Unable to perform insert: $query"); $this->a_rows = @mysql_affected_rows($this->id); } function Update ($query) { $this->result = @mysql_query($query, $this->id) or MySQL_ErrorMsg ("Unable to perform update: $query"); $this->a_rows = @mysql_affected_rows($this->id); } function Delete ($query) { $this->result = @mysql_query($query, $this->id) or MySQL_ErrorMsg ("Unable to perform Delete: $query"); $this->a_rows = @mysql_affected_rows($this->id); } function Getsql_error() { $this->error[0] = mysql_errno(); $this->error[1] = mysql_error(); return $this->error; } function ServerDisconnect () { mysql_close($this->id); } function Freemem() { mysql_free_result($this->result); } /* End CLASS definition */ } /* ******************************************************************** * MySQL_ErrorMsg * * Print out an MySQL error message * */ function MySQL_ErrorMsg ($msg) { # Close out a bunch of HTML constructs which might prevent # the HTML page from displaying the error text. // echo("</ul></dl></ol>n"); //echo("</table></script>n"); # Display the error message $text = "<font color="#ff0000"><p>Error: $msg :<br>"; $text .= mysql_error(); $text .= "</font><p>"; echo ($text); // die ($text); } ?> Link to comment https://forums.phpfreaks.com/topic/112-mysql-result-cant-be-returned-from-class/#findComment-338 Share on other sites More sharing options...
metalblend Posted January 31, 2003 Author Share Posted January 31, 2003 ..well you definitely have more knowledge of classes than I do. I understand it but not to the point where I can recreate it. Link to comment https://forums.phpfreaks.com/topic/112-mysql-result-cant-be-returned-from-class/#findComment-340 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.