Home > Backend Development > PHP Tutorial > Basic principles and examples of PHP database field reuse_PHP tutorial

Basic principles and examples of PHP database field reuse_PHP tutorial

WBOY
Release: 2016-07-21 15:26:35
Original
845 people have browsed it

1. Basics of logical algebra:
1. Numbers are represented in binary, and the only possible numbers are 0 and 1.
2. There are only three basic operations: "and", "or" and "not".
 The AND operation is defined as: (Use & to represent the AND operation)
 0 & 0 = 0
 0 & 1 = 0
 1 & 0 = 0
 1 & 1 = 1
It can be simply understood as: as long as there is a 0, the result is 0, similar to multiplication.
 OR operation is defined as: (Using expression AND operation)
 0 0 = 0
 0 1 = 1
 1 0 = 1
 1 1 = 1
 It can be simply understood as: As long as there is a 1, the result is 1, similar to addition.
2. Logical operation examples:
0111101010101010111111111111111 & 1100000 = 1100000
Generally it can be understood as:
If you want to get the N-digit value of a number, you only need to add this number Just perform an AND operation with 2 raised to the N-1 power (mask).
3. Database field definition:
Take data table binary_sample as an example:
create table binary_sample(
uid int unsigned not null,
status int unsigned not null default 0,
primary key(uid),
key i_s(status)
)engine=innodb;
status field definition:
The status field data type is a 32-bit integer, in order to be as possible as possible To store multiple attributes, we define them as follows:
All the following "bits" are described in order from low to high (right to left).
0-2 digits represent user registration status:
000 represents new registration not approved
001 represents registration approved
010 represents advanced user
011 represents administrator
100 represents Super Administrator
 101 Reserved
 110 Reserved
 111 Mask
 3-5 User Gender:
 000 means the gender is uncertain
 001 means the gender is male
 010 means Gender is female
011 Reserved
100 Reserved
101 Reserved
110 Reserved
111 Mask
If we want to query all male users:
select * from binary_sample where status & b'111000' = b'001000';
If we want to query all administrator users:
select * from binary_sample where status & b'111' = b'011';
If we want To query all male administrator users:
select * from binary_sample where status & b'111111' = b'001011';
If we want to query all non-newly registered and unapproved users:
select * from binary_sample where status & b'111' != b'000';
Fourth, use PHP program to perform such calculations:
define("USER_NEW",0);//000
Define("USER_NORMAL",1);//001
define("USER_ADVANCE",2);//010
define("USER_MANAGE",3);//011
define( "USER_SUPER",4);//100
define("USER_MASK",7);//111
define("GENDER_UNKNOWN",0);// 000000
define("GENDER_MALE",8 );// 001000
Define("GENDER_FEMALE",9);// 010000
define("GENDER_MASK",56);// 111000
If we want to query all male users:
$status=GENDER_MALE;
$mask=GENDER_MASK;
$sql="select * from binary_sample where status & ${mask}' = ${status}";
If we want to query all administrator users Then:
$status=USER_MANAGE;
$mask=USER_MASK;
$sql="select * from binary_sample where status & ${mask}' = ${status}"; Query all male administrator users:
$status=GENDER_MALE & USER_MANAGE;
$mask = GENDER_MASK & GENDER_MASK;
$sql="select * from binary_sample where status & ${mask}' = ${ status}";
If we want to query all non-newly registered and unapproved users:
$status = USER_NEW;
$mask = USER_MASK;
$sql="select * from binary_sample where status & ${mask} != ${status}";
By analogy, as long as the meaning of each value is defined, the query is basically fixed.

http://www.bkjia.com/PHPjc/323920.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/323920.htmlTechArticle1. Basics of logical algebra: 1. Numbers are represented in binary, and the only possible numbers are 0 and 1. . 2. There are only three basic operations: "and", "or" and "not". AND operation definition...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template