Jump to content

Object Oriented PHP script problem


egorig

Recommended Posts

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

Link to comment
Share on other sites

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.

 

 

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.