Home Backend Development PHP Tutorial 在PHP中操作MySQL要注意哪些问题?_PHP

在PHP中操作MySQL要注意哪些问题?_PHP

Jun 01, 2016 pm 12:30 PM
Which operate Notice question

  1、分号的例外

在MySQL中,每一行命令都是用分号(;)作为结束的,但是当一行MySQL命令被插入在PHP代码中时,最好把后面的分号省略掉,例如:

mysql_query ("INSERT INTO tablename (first_name, last_name) VALUES ('$first_name', '$last_name')");

这是因为PHP也是以分号作为一行的结束的,额外的分号有时会导致PHP 的语法分析器做出错误的分析,所以还是省略掉的好。在这种情况下,虽然省略了分号,但是 PHP 在执行 MySQL 命令时会自动加上分号。

另外还有一个不要加分号的情况。当你想把要字段的竖着排列显示下来,而不是像通常的那样横着排列时,你可以用 \G 来结束一行SQL 语句,这时就用不上分号了,例如:

    SELECT * FROM PENPALS WHERE USER_ID = 1\G

  2、TEXT、DATE、和 SET 数据类型

  TEXT 不是一种数据类型,应该是" LONG VARCHAR "或者" MEDIUMTEXT "。

  DATE 数据类型的格式是 YYYY-MM-DD ,比如: 2001-10-01 。你可以很容易的用 date 函数来得到这种格式的当前系统时间:

    date("Y-m-d")

  并且,在 DATA 数据类型之间可以作减法,得到相差的时间天数:

    $age = ($current_date - $birthdate);

  集合 SET 是一个有用的数据类型,它和枚举 ENUM 有点相似,只不过是 SET 能够保存多个值而 ENUM 只能保存一个值而已。而且, SET 类型最多只能够有 64 个预定的值,而 ENUM 类型却能够处理最多 65,535 个预定义的值。而如果需要有大于 64 个值的集合,该怎么办呢?这时就需要定义多个集合来一起解决这个问题了。

  3、通配符

  SQL 的通配符有两种:" * "和" % "。分别用在不同的情况下。例如:如果你想看到数据库的所有内容,可以像这样来查询:

    SELECT * FROM dbname WHERE USER_ID LIKE '%';

  这儿,两个通配符都被用上了。他们表示相同的意思 ?? 都是用来匹配任何的字符串,但是他们用在不同的上下文中。" * "用来匹配字段名,而" % "用来匹配字段值。另外一个不容易引起注意的地方是" % "通配符需要和 LIKE 关键字一起使用。

  还有一个通配符,就是下划线" _ ",它代表的意思和上面不同,是用来匹配任何单个的字符的。

  4、NOT NULL 和空记录

  如果用户在没有填任何东西的情况下按了 submit 按钮,会怎样呢?如果你确实需要一个值,那么可以用客户端脚本或者服务器端脚本来进行数据验证,这一点在前面已经说过了。但是,在数据库中却是允许一些字段被空出来什么也不填。对此类纪录, MySQL 将进行以下步骤:

  插入值 NULL ,这是缺省的操作。

  如果你在字段定义中为之声明了 NOT NULL (在建立或者修改这个字段的时候), MySQL 将把这个字段空出来什么东西也不填。

  对于一个 ENUM 枚举类型的字段,如果你为之声明了 NOT NULL , MySQL 将把枚举集的第一个值插入到字段中。也就是说, MySQL 把枚举集的第一个值作为这个枚举类型的缺省值。

  一个值为 NULL 的纪录和一个空纪录是有一些区别的。 % 通配符可以匹配空纪录,但是却不能匹配 NULL 纪录。在某些时候,这种区别会造成一些意想不到的后果。就经验而言,任何字段都应该声明为 NOT NULL 。这样下面的 SELECT 查询语句就能够正常运转了:

  if (!$CITY) {$CITY = "%";}
  $selectresult = mysql_query ("SELECT * FROM dbname
  WHERE FIRST_NAME = ' Bill '
  AND LAST_NAME = ' Gates '
  AND CITY LIKE '$CITY'
  ");

  在第一行中,如果用户没有指定一个 CITY 值,那么就会用通配符 % 来代入 CITY 变量,这样搜索时就会把任何的 CITY 值都考虑进去,甚至包括那些 CITY 字段为空的纪录。

  但是如果有一些纪录,它的 CITY 字段值是 NULL ,这时问题就出现了。上面的查询是不能够找到这些字段的。问题的一个解决办法可以是这样:

  if (!$CITY) {$CITY = "%";}
  $selectresult = mysql_query ("SELECT * FROM dbname
  WHERE FIRST_NAME = ' Bill '
  AND LAST_NAME = ' Gates '
  AND (CITY LIKE '$CITY' OR CITY IS NULL)
  ");

  注意在搜索 NULL 时,必须用" IS "关键字,而 LIKE 时不会正常工作的。

  在最后要提到的是,如果你在加入或者修改一个新的字段之前,数据库中已经有了一些记录了,这时新加入的字段在原来的纪录中的值,可能是 NULL ,也可能为空,在这种情况下,使用 SELECT 查询要特别的小心。

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PyCharm usage tutorial: guide you in detail to run the operation PyCharm usage tutorial: guide you in detail to run the operation Feb 26, 2024 pm 05:51 PM

PyCharm is a very popular Python integrated development environment (IDE). It provides a wealth of functions and tools to make Python development more efficient and convenient. This article will introduce you to the basic operation methods of PyCharm and provide specific code examples to help readers quickly get started and become proficient in operating the tool. 1. Download and install PyCharm First, we need to go to the PyCharm official website (https://www.jetbrains.com/pyc

What is sudo and why is it important? What is sudo and why is it important? Feb 21, 2024 pm 07:01 PM

sudo (superuser execution) is a key command in Linux and Unix systems that allows ordinary users to run specific commands with root privileges. The function of sudo is mainly reflected in the following aspects: Providing permission control: sudo achieves strict control over system resources and sensitive operations by authorizing users to temporarily obtain superuser permissions. Ordinary users can only obtain temporary privileges through sudo when needed, and do not need to log in as superuser all the time. Improved security: By using sudo, you can avoid using the root account during routine operations. Using the root account for all operations may lead to unexpected system damage, as any mistaken or careless operation will have full permissions. and

Linux Deploy operation steps and precautions Linux Deploy operation steps and precautions Mar 14, 2024 pm 03:03 PM

LinuxDeploy operating steps and precautions LinuxDeploy is a powerful tool that can help users quickly deploy various Linux distributions on Android devices, allowing users to experience a complete Linux system on their mobile devices. This article will introduce the operating steps and precautions of LinuxDeploy in detail, and provide specific code examples to help readers better use this tool. Operation steps: Install LinuxDeploy: First, install

What to do if you forget to press F2 for win10 boot password What to do if you forget to press F2 for win10 boot password Feb 28, 2024 am 08:31 AM

Presumably many users have several unused computers at home, and they have completely forgotten the power-on password because they have not been used for a long time, so they would like to know what to do if they forget the password? Then let’s take a look together. What to do if you forget to press F2 for win10 boot password? 1. Press the power button of the computer, and then press F2 when turning on the computer (different computer brands have different buttons to enter the BIOS). 2. In the bios interface, find the security option (the location may be different for different brands of computers). Usually in the settings menu at the top. 3. Then find the SupervisorPassword option and click it. 4. At this time, the user can see his password, and at the same time find the Enabled next to it and switch it to Dis.

Huawei Mate60 Pro screenshot operation steps sharing Huawei Mate60 Pro screenshot operation steps sharing Mar 23, 2024 am 11:15 AM

With the popularity of smartphones, the screenshot function has become one of the essential skills for daily use of mobile phones. As one of Huawei's flagship mobile phones, Huawei Mate60Pro's screenshot function has naturally attracted much attention from users. Today, we will share the screenshot operation steps of Huawei Mate60Pro mobile phone, so that everyone can take screenshots more conveniently. First of all, Huawei Mate60Pro mobile phone provides a variety of screenshot methods, and you can choose the method that suits you according to your personal habits. The following is a detailed introduction to several commonly used interceptions:

How to solve the problem that jQuery cannot obtain the form element value How to solve the problem that jQuery cannot obtain the form element value Feb 19, 2024 pm 02:01 PM

To solve the problem that jQuery.val() cannot be used, specific code examples are required. For front-end developers, using jQuery is one of the common operations. Among them, using the .val() method to get or set the value of a form element is a very common operation. However, in some specific cases, the problem of not being able to use the .val() method may arise. This article will introduce some common situations and solutions, and provide specific code examples. Problem Description When using jQuery to develop front-end pages, sometimes you will encounter

Detailed explanation of the role of .ibd files in MySQL and related precautions Detailed explanation of the role of .ibd files in MySQL and related precautions Mar 15, 2024 am 08:00 AM

Detailed explanation of the role of .ibd files in MySQL and related precautions MySQL is a popular relational database management system, and the data in the database is stored in different files. Among them, the .ibd file is a data file in the InnoDB storage engine, used to store data and indexes in tables. This article will provide a detailed analysis of the role of the .ibd file in MySQL and provide relevant code examples to help readers better understand. 1. The role of .ibd files: storing data: .ibd files are InnoDB storage

Discuz domain name modification operation guide Discuz domain name modification operation guide Mar 09, 2024 pm 04:36 PM

Discuz Domain Name Modification Operation Guide In the process of using the Discuz forum system, sometimes we need to modify the domain name of the forum. It may be because the domain name needs to be changed, or some domain name resolution problems need to be repaired. This article will introduce in detail how to modify the domain name in the Discuz forum system, and give some specific code examples. 1. Back up data Before performing any operation, we must back up the data to prevent data loss due to operational errors. In Discuz, you can use the background data backup

See all articles