简体   繁体   English

从php查询数据库

[英]Querying database from php

I have a following structure of my database: 我的数据库具有以下结构:

在此处输入图片说明

And I have created a class User within php which has following attributes: 我在php中创建了一个User类,它具有以下属性:

public $user_id;
public $first_name;
public $last_name;
public $password;
public $job;
public $department;

Then in my code, I plan to have $job and $department to be objects called Job and Department. 然后在我的代码中,我计划将$ job和$ department称为Job and Department。

Within a user class, I have method called load user data. 在用户类中,我有一种称为加载用户数据的方法。 I get all user data first but then I struggled how should I do my queries to get information for department and job(by id) should I have one query(using joints) to load all the data and create my object from that data, or should I run queries for Job and Department like that 我首先获取所有用户数据,但是随后我就很难进行查询,以获取部门和工作的信息(按ID),如果我有一个查询(使用关节)来加载所有数据并根据该数据创建对象,或者我应该像这样运行Job和Department的查询吗

    $this->department = new Department($this->dbc, $userData['department_id']);
    $this->job = new Job($this->dbc, $userData['job_id']);

Where within each of the object, query is executed and retrieves the data? 在每个对象中的哪个位置执行查询并检索数据?

If you want to have Job and Department be an object, you are looking at using either method. 如果要让Job和Department作为对象,则可以使用这两种方法。 If you choose to use joins in your SQL query, you will be working on just one object, containing the entire result set of your SQL query meaning you will only need to open and close one connection. 如果选择在SQL查询中使用联接,那么您将只处理一个对象,其中包含SQL查询的整个结果集,这意味着您只需要打开和关闭一个连接即可。 If you choose not to use the joins, you would be returning your id fields to then query the Job and Department tables, opening and closing at least two more connections. 如果选择不使用联接,则将返回id字段,然后查询Job和Department表,从而打开和关闭至少两个连接。

Personally, I would use one query, return one array to be kept in memory and then have your Job and Department objects just pull from that array. 就个人而言,我将使用一个查询,返回一个要保留在内存中的数组,然后将Job和Department对象从该数组中拉出。 It keeps the number of connections down and your processing requirements lower. 它可以减少连接数量,降低您的处理要求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM