Home Database Mysql Tutorial PHP中操作MySQL的需注意的问题_MySQL

PHP中操作MySQL的需注意的问题_MySQL

Jun 01, 2016 pm 01:52 PM
yes most

  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 = 1G

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

  MySQL 数据表的字段必须有定义一个数据类型。这有大约 25 种选择,大部分都是直接明了的,就不多费口舌了。但有几个有必要提一下。

  TEXT 不是一种数据类型,虽然可能有些书上是这么说的。它实际上应该是“ LONG VARCHAR ”或者“ MEDIUMTEXT ”。

  DATE 数据类型的格式是 YYYY-MM-DD ,比如: 1999-12-08 。你可以很容易的用 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 = ' 柳 '

AND LAST_NAME = ' 如风 '

AND CITY LIKE '$CITY'

");

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

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

if (!$CITY) { $CITY = "%"; }

$selectresult = mysql_query ("SELECT * FROM dbname

WHERE FIRST_NAME = ' 柳 '

AND LAST_NAME = ' 如风 '

AND (CITY LIKE '$CITY' OR CITY IS NULL)

");

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

  在最后要提到的是,如果你在加入或者修改一个新的字段之前,数据库中已经有了一些记录了,这时新加入的字段在原来的纪录中的值,可能是 NULL ,也可能为空。这也算是 MySQL 的一个 Bug 吧,所以在这种情况下,使用 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)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months 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)

Please recommend a cost-effective 1155-pin CPU Please recommend a cost-effective 1155-pin CPU Jan 14, 2024 pm 01:30 PM

Please recommend which 1155-pin CPU is the best. The current 1155-pin CPU with the highest performance is Intel Corei7-3770K. It has 4 cores and 8 threads, a base frequency of 3.5GHz, and supports TurboBoost2.0 technology, which can reach up to 3.9GHz. In addition, it is equipped with 8MB of level 3 cache and is an excellent processor with the LGA1155 pin, the most powerful CPU Intel Core i73770K. The LGA1155 interface is the interface type used by second and third generation Core processors. The best performing one is Intel Core i73770K. The parameters of this processor are as follows: 1. Applicable type: desktop; 2. CPU series: Core i7; 3. CPU

What is NEAR Protocol Coin? What are the characteristics of NEAR Protocol coins? What is NEAR Protocol Coin? What are the characteristics of NEAR Protocol coins? Mar 04, 2024 pm 11:20 PM

NEARProtocol: A scalable, user-friendly blockchain platform NEARProtocol is a blockchain platform using sharding technology designed to address the challenges faced by blockchain technology in terms of scalability, user-friendliness, and security. It provides developers with an efficient and user-friendly platform that enables them to easily build and deploy decentralized applications (dApps). NEARProtocol is designed to lower the barriers to blockchain development while providing a high degree of efficiency and security. By adopting sharding technology, NEARProtocol can better handle large-scale transactions and provide users with faster transaction confirmation times. Overall, NEARProtocol is designed to provide

What is the number of pins in the Antec 650 power supply motherboard cable interface? (Antec 650 power supply wiring diagram) What is the number of pins in the Antec 650 power supply motherboard cable interface? (Antec 650 power supply wiring diagram) Jan 03, 2024 am 10:46 AM

How many pins does the Antec 650w motherboard cable have? The power cable of the Antec 650W power supply motherboard is usually 24 pins, which is the largest power interface on the motherboard. Its function is to connect the motherboard and power supply to provide power to the motherboard and other system components. In addition, the Antec 650W power supply may also include other types of power interfaces, such as CPU8pin, PCIe6+2pin, etc., for connecting other components such as the CPU and independent graphics cards. Motherboard routing tutorial Motherboard routing is the process of connecting circuits between various electronic components when designing a motherboard. In this process, factors such as circuit stability, signal transmission speed and accuracy need to be considered. When routing wiring according to the circuit diagram, pay attention to the layout and select appropriate line width and distance to avoid

What are the best Python resources? What are the best Python resources? Sep 16, 2023 pm 02:29 PM

Resources for any programming language include video lessons, notes, and e-books. Here I'll list the best resources for Python. Python official documentation Many websites provide Python resources, but the official documentation is the best. Let’s take a look at the resources they offer. Python Beginners Guide - https://wiki.python.org/moin/BeginnersGuide Python Developer Guide - https://devguide.python.org/Free Python Books − https://wiki.python.org/moin/PythonBooksPyth

Which graphics card interface works best? Which graphics card interface works best? Feb 22, 2024 am 10:51 AM

With the rapid development of computer technology, graphics cards, as one of the important components of computers, play a pivotal role in games, graphic design and other fields. The graphics card interface is the bridge connecting the graphics card and the motherboard, affecting the performance and effect of the graphics card. So, which graphics card interface works best? Currently, there are three common graphics card interfaces on the market: PCI, AGP, and PCIe. Among them, the PCI interface is an early standard interface and is relatively outdated. For some old computers or simple office needs

Which is the best air cooling radiator? Which is the best air cooling radiator? Jan 25, 2024 am 11:06 AM

When choosing a computer, most users now pay attention to the heat dissipation problem. Therefore, the current air-cooled radiators are very popular because they are affordable and cost-effective. However, most users do not quite understand which one is best. According to the current situation, The best is Valkyrie. Which air-cooled radiator is the best: A: According to the current ladder chart, the best one is Valkyrie’s “vk Star Ring GL360”. The performance of this water cooling is explosive, and the price is very good. For entry-level users, it will definitely not suffer. Related introduction of vk Starring GL360: 1. This water-cooled product looks very good, and the installation is relatively simple. It is also very convenient for novices to operate. 2. The fan is very simple overall. The logo also has a rotating circle, which is boring.

Which website is the best to download win10 Which website is the best to download win10 Jun 29, 2023 pm 07:33 PM

Which website is the best to download win10? Nowadays, many friends like to download and install or reinstall the system online. This requires an excellent download platform. So which website is good for downloading the win10 system? Many friends don’t know how to operate in detail. The editor has compiled the download websites for the win10 system below. Good introduction, if you are interested, follow the editor to read below! A good introduction to win10 system download website Answer: System 520 is the best. The systems here are safe and reliable, and the installation method is also very simple, suitable for all users. 1. The system here has several benefits. 2. First of all, ensure safety and reliability. All systems can be installed normally without errors. 3. Secondly, the installation operation is very simple, and the system is installed with one click.

How to choose the best computer configuration How to choose the best computer configuration Feb 21, 2024 am 11:18 AM

How to choose the best computer configuration With the continuous advancement of technology, computers have become an indispensable part of our lives. They not only meet our work needs, but also bring us entertainment and leisure. When choosing a computer, excellent configuration is very important. This article will introduce you to some important factors and tips when purchasing computer configurations. First, we need to consider our own needs. Different people use computers for different purposes. Some people mainly use them for daily office work and Internet browsing, while others may need high-performance games or professional

See all articles