从sql数据库中提取数据的过程 [英] Process for pulling data from a sql database

查看:260
本文介绍了从sql数据库中提取数据的过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想制作一个程序,在将数据返回给用户之前,从 sql 数据库中提取对象并根据一组函数处理这些对象.

I want to make a program that pulls objects from a sql database and processes the objects against a set of functions before returning the data to the user.

它会像这样工作:

  1. 用户以 JavaScript 形式指定日期范围.
  2. 日期范围被发送到服务器上的一个 php 文件,该文件将其转换为一个 sql 查询.
  3. sql查询拉取数据库中与日期匹配的所有数据范围,并将每个匹配项及其相关信息存储为一个 php临时 php 文件中的类(参见下面的示例).
  4. 主 php 程序然后根据一组函数处理临时 php 文件中的每个类,并将信息返回给用户.

示例临时 php 文件:

Example temporary php file:

class ice_cream
{
       $temperature = 0;
       $texture = 'soft';
}

class hard_candy
{
    $temperature = 20;
    texture = 'hard';
}

用于处理类的函数示例:

Example of Functions to process the classes against:

function will_it_break_dentures($class_name)
{
    //$class_name is passed to the function based on the classes available in temp file
    $food_type_class = new $class_name();
    $damage_potential_temperature = $food_type_class->temperature;
    $damage_potential_temperature = $food_type_class->texture;

    if($damage_potential_temperature >= 15) && ($damage_potential_texture === 'hard')
    {
        print("Don't eat it");
    }

}

是否有更有效/更安全的方法从数据库中提取数据并针对一组函数进行处理?这是从数据库中提取数据进行处理的标准方法吗?第 3 项似乎可疑,但这是我能想到的最好的方法.感谢您提供有关最佳实践的任何相关信息资源.

Is there a more efficient/secure way to pull data from a database and process it against a set of functions? Is this the standard way to pull data from a database for processing? Item #3 seems suspect, but it's the best I could come up with. Any relevant informational resources on best practice are appreciated.

推荐答案

你的类定义应该定义对象的属性,而不是值.它应该静态定义,而不是在运行时动态定义.在运行时,您创建此类的实例并为每个实例填充数据.

Your class definition should define the properties of the object, not the values. It should be defined statically, not dynamically at runtime. At runtime you create instances of this class and populate the data for each instance.

尝试这样的事情:

class Food {
   var $name;
   var $temp;
   var $texture;

   // Constructor
   public function food($name, $temp, $texture) {
      $this->name = $name;
      $this->temp = $temp;
      $this->texture = $texture;
   }

   public function breaksDentures() {
      if($this->temp >= 15) && ($this->texture === 'hard')
         return true;
      else
         return false;
   }
}

像这样使用它:

function processFoods($daterange) {
   $query = build_query_from_daterange($daterange);
   $result = mysql_query($query);

   while($row = mysql_fetch_assoc($result)) {
      $food = new Food($row['name'], $row['temp'], $row['texture']);
      // Now $food is an instance of the Food class populate with the values
      // from the db, e.g., 'ice cream', 0, 'hard'.

      if $food->breaksDentures() {
         print("Don't eat it");
      }
   }
}

P.S. 您可能想复习一下面向对象的概念.您的问题很好,但表明您对 OO 基础知识有些困惑.

P.S. You may want to brush up on your object-oriented concepts. Your question is a good one but indicates some confusion about OO basics.

这篇关于从sql数据库中提取数据的过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆