class db
{
private $db;
public function __construct()
{
$this->db = mysql_connect('host', 'user', 'pass');
$db = mysql_select_db('db_name', $this->db);
}
public function query($qry)
{
return mysql_query($qry, $this->db);
}
//////////////////////////////////
// Clean SQL Variables (Security Function)
////////
function mySQLSafe($value, $quote="'") {
// Stripslashes
if (get_magic_quotes_gpc())
{
$value = stripslashes($value);
}
// Quote value
if (version_compare(phpversion(),"4.3.0")=="-1")
{
$value = mysql_escape_string($value);
}
else
{
$value = mysql_real_escape_string($value, $this->db);
}
$value = $quote . $value . $quote;
return $value;
}
public function close()
{
mysql_close($this->db);
}
} // End db class
class products
{
public function returnProductQueryFields() {
$fields = "
`p`.`sku` AS `sku`,
`p`.`productTitle` AS `name`,
`p`.`productDescTitle` AS `title`,
`p`.`productShortDescription` AS `desc`,
`p`.`productLongDescription` AS `html`,
`p`.`bullets` AS `bullets`,
`p`.`stock` AS `stock`,
`p`.`minPurchaseQty` AS `minQty`,
`p`.`dimensions` AS `dims`,
`p`.`weight` AS `weight`,
`p`.`barcode` AS `barcode`,
`p`.`productBrand` AS `productBrand`,
`p`.`mpn` AS `mpn`,
`p`.`colour` AS `colour`,
`p`.`dateAdded` AS `dateAdded`,
`p`.`dateDue` AS `dateDue`,
`p`.`priceCost` AS `priceCost`,
`p`.`priceCostSpecial` AS `priceCostSpecial`
";
return $fields;
}
/// This builds a product query based on the variables sent to it.
public function returnProductQuery($keywords, $catId = 0, $manId = 0, $new = 0, $special = 0)
{
global $glob;
$db = new db();
$productQuery = sprintf("SELECT %s FROM %s AS `p`
INNER JOIN %s AS `c` ON (`p`.`sku` = `c`.`sku`) WHERE ",
self::returnProductQueryFields(), $glob['table_products'], $glob['table_categories_idx']);
if( isset($keywords) && !empty($keywords) )
// using keywords.
{
$productQuery .= "
(`p`.`sku` = '%".$db->mySQLSafe($keywords, "")."%' OR
`p`.`productTitle` = '%".$db->mySQLSafe($keywords, "")."%' OR
`p`.`productDescTitle` = '%".$db->mySQLSafe($keywords, "")."%' OR
`p`.`productShortDescription` = '%".$db->mySQLSafe($keywords, "")."%' OR
`p`.`productLongDescription` = '%".$db->mySQLSafe($keywords, "")."%' OR
`p`.`mpn` = '%".$db->mySQLSafe($keywords, "")."%') AND";
}
if ( isset($catId) && $catId != 0 && $catId != "" )
{
$productQuery .= " `c`.`catId` = " . $db->mySQLSafe($catId) . " AND";
}
if ( isset($manId) && $manId != 0 && $manId != "" )
{
$productQuery .= " `p`.`manId` = " . $db->mySQLSafe($manId) . " AND";
}
if ( isset($special) && $special != 0 && $special != "" )
{
$productQuery .= " `p`.`priceCostSpecial` != '0' AND";
}
$productQuery = substr($productQuery,0,strlen($productQuery)-6);
/*if ( isset($new) && $new != 0 )
{
$product_query = " `p`.`date_added` " . $db->mySQLSafe($_GET['man_id']) . " AND";
}*/
$productQuery .= " AND `web` = '1' ORDER BY `p`.`priceCost` ASC, `p`.`productTitle` ASC ";
$db->close();
//echo $product_query;
return $productQuery;
}// end function.
public function returnProducts($catId=0, $keywords='', $manId=0, $new=0, $special=0)
{
$output = array();
$query = self::returnProductQuery($keywords, $catId, $manId, $new, $special);
$db = new db();
$items = $db->select($query);
for ($n=0+$start; $n < mysql_num_rows($result); $n++)
{
$row = mysql_fetch_assoc($result);
$output[$n] = $row;
}
$db->close();
return $output;
}
} // End class products
// Then this is what I am trying to achieve....
$items = products::returnProducts($_GET['catId']);
$table = "<table>";
for ($i=0; $i<count($items); $++)
{
$table .= "<tr>
<td>";
$table .= $items[$i]['productName'];
$table .= "</td>
</tr>";
}
$table .= "</table>";
echo $table;
I think... I think, returnProductQuery is using the same link resource as returnProducts as returnProductQuery seems to $db->close() the db connection, then returnProducts opens it again with $db = new db();
However, when I look at the errors it seems that returnProducts is trying to use the same mysql resource id as the one returnProductQuery created.
Do classes inherit other classes' resources? - That is stupid, the whole concept of oop is that each class (regardless of whether it's initiaised or not) is it's own entity and has it's own internal resources.
In fact, it should never inherit anything unless you specifically send the resource or object to the respective class.
Does this make any sense to anyone?