juliusyves Posted July 11, 2012 Share Posted July 11, 2012 Here are my OOP methods from different classes, late static binding is implemented. Kindly help me implement mysql_num_rows. Thanks guys public function num_rows($result_set) { return mysql_num_rows($result_set); } public function query($sql) { $this->last_query = $sql; $result = mysql_query($sql, $this->connection); $this->confirm_query($result); return $result; } public static function find_all() { return static::find_by_sql("SELECT * FROM ".static::$table_name); } public static function find_by_sql($sql="") { global $database; $result_set = $database->query($sql); $object_array = array(); while ($row = $database->fetch_array($result_set)) { $object_array[] = static::instantiate($row); } return $object_array; } private static function instantiate($record) { $class_name = get_called_class(); $object = new $class_name; foreach($record as $attribute=>$value){ if($object->has_attribute($attribute)) { $object->$attribute = $value; } } return $object; } <?php // Find all the car $car_set = Car::find_all(); $car_count = $database->num_rows($car_set); echo $car_count; ?> Quote Link to comment https://forums.phpfreaks.com/topic/265500-warning-mysql_num_rows-expects-parameter-1-to-be-resource/ Share on other sites More sharing options...
requinix Posted July 11, 2012 Share Posted July 11, 2012 $car_count = count($car_set); Otherwise having num_rows() as an instance method is decent (if you pass to it the return value from query()). Quote Link to comment https://forums.phpfreaks.com/topic/265500-warning-mysql_num_rows-expects-parameter-1-to-be-resource/#findComment-1360695 Share on other sites More sharing options...
smoseley Posted July 11, 2012 Share Posted July 11, 2012 You're not passing it a resource, you're passing it an array. I suggest that you persist the $result_set resource from your query function, and eliminate the parameter requirement in num_rows(), having it use the persisted resource instead. Quote Link to comment https://forums.phpfreaks.com/topic/265500-warning-mysql_num_rows-expects-parameter-1-to-be-resource/#findComment-1360735 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.