egorig Posted July 8, 2007 Share Posted July 8, 2007 db.php <?php class MySQL { var $connection; function MySQL() { $this->connection = mysql_connect(DB_HOST, DB_USER, DB_PASS) or die(mysql_error()); mysql_query("set names cp1251"); mysql_select_db(DB_NAME, $this->connection) or die(mysql_error()); } function getQuickSearchOffers($city,$type) { $q = "SELECT pid,ptype,pobject,pcity,pprice,pdate,ptop FROM ".TBL_PRODUCTS." WHERE ptype like '".$type."' AND pcity like '".$city."' ORDER BY pid desc LIMIT 5"; $result = mysql_query ($q, $this->connection) or die(mysql_error()); return $result; } function getNumRows($query) { $result = mysql_num_rows($query) or die (mysql_error()); return $result; } } $server = new MySQL(); ?> in another php file, firstly i include the db.php and then I'm calling the getQuickSearchOffers($city,$type) function like this $data = $server->getQuickSearchOffers(1,2); and then $rows = $server->getNumRows($data); The problem: if the query return 1 or more rows , when i echo the $rows variable it displays the correct amount of rows. If the query do not returns any result, when i echo $rows it doesn't return "0". If i add "echo mysql_num_rows($result)" in the getQuickSearchOffers(1,2) function, function getQuickSearchOffers($city,$type) { $q = "SELECT pid,ptype,pobject,pcity,pprice,pdate,ptop FROM ".TBL_PRODUCTS." WHERE ptype like '".$type."' AND pcity like '".$city."' ORDER BY pid desc LIMIT 5"; $result = mysql_query ($q, $this->connection) or die(mysql_error()); echo mysql_num_rows($result) return $result; } no matter if there is or there is not a result it echos the correct amount of rows (0,1,2,3,4...). Why is that happening? How can i fix this? By function getNumRows i want to check if the query returns any result, and if it doesn't i want to display a proper message. Thanks in advanced to everyone! EDITED BY WILDTEEN88: Please use code tags ( ) when posting code. Thank you Quote Link to comment Share on other sites More sharing options...
steelmanronald06 Posted July 8, 2007 Share Posted July 8, 2007 Did you create an instance of the class in the new file? It should look like this: <?php include ("db.php"); $server = new MySQL; // This is case-sensitive $data = $server->getQuickSearchOffers(1,2); $rows = $server->getNumRows($data); ?> Quote Link to comment Share on other sites More sharing options...
egorig Posted July 8, 2007 Author Share Posted July 8, 2007 firstly i include the db.php and then Yes, but the problem's still there. I tried almost everything... Quote Link to comment Share on other sites More sharing options...
keeB Posted July 12, 2007 Share Posted July 12, 2007 How about you modify your code to look like this: <?php class MySQL { var $connection; function MySQL() { $this->connection = mysql_connect(DB_HOST, DB_USER, DB_PASS) or die(mysql_error()); mysql_query("set names cp1251"); mysql_select_db(DB_NAME, $this->connection) or die(mysql_error()); } function getQuickSearchOffers($city,$type) { $q = "SELECT pid,ptype,pobject,pcity,pprice,pdate,ptop FROM ".TBL_PRODUCTS." WHERE ptype like '".$type."' AND pcity like '".$city."' ORDER BY pid desc LIMIT 5"; $result = mysql_query ($q, $this->connection) or die(mysql_error()); $this->numRows = mysql_num_rows($result) return $result; } function getNumRows($query) { return $this->numRows; } } $server = new MySQL(); ?> It doesn't really answer your question, but it's a simple workaround. 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.