Jump to content

Variable Validation OOP


bdmovies

Recommended Posts

Hello,

 

I have a class that interacts with my database (simple enough)

 

but for this particular class, the only input it is taking will be whole numbers, so I thought perhaps doing this word work:

 

<?php
class myClass extends database {
   function myClass($whole_num) {
      number validation here...
      if number return true
      if not number return "Error"
   }

   function getSomeValueFromDatabase($whole_num) {
      interact with database here...
   }
}
?>

 

However this did not work, I received a "No Database Selected" error.

 

Besides from writing an entire validation class, is there any way to get this job done using the basic idea laid out above?

Link to comment
https://forums.phpfreaks.com/topic/151184-variable-validation-oop/
Share on other sites

<?php
require_once ('database.class.php');

class serviceInformation extends database {

function serviceInformation($serviceID) {
	if(!is_numeric($serviceID)) {
		return "Error";
	}
	else {
		return true;
	}
}

function getServiceInformation($serviceID) {
	if(!is_numeric($serviceID)) {
		return "Error";
	}
	$sql = "SELECT * FROM serviceInformation WHERE serviceID = '$serviceID'";
	$result = $db->query($sql);
}

function getManner($serviceID) {
	$sql = "SELECT serviceType FROM serviceInformation WHERE serviceID = '$serviceID'";
	$result = $this->query($sql);
	$row = $this->fetchAssoc($result);
	$serviceType = $row['serviceType'];
	$sql = "SELECT * FROM manners WHERE id = '$serviceType'";
	$result = $this->query($sql);
	$row = $this->fetchAssoc($result);
	$manner = $row['manner'];
	return $manner;
}
}

?>

 

Returns:

No database selected

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /Applications/MAMP/htdocs/Zend_Workspace/Fusion/classes/database.class.php on line 57

 

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /Applications/MAMP/htdocs/Zend_Workspace/Fusion/classes/database.class.php on line 57

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.