Feed on
Subscription

A few wants an attention issues of MySQL are handled in PHP

? to MySQL,The first each travel command that what you must remember well is it is to use semicolon (;) serves as an end,But... without completely very thing,Also be same here,When a MySQL is inserted to be in PHP code,Had better drop the semicolon from the back elliptically,For example:

Mysql_query ("INSERT INTO Tablename (first_name, last_name) VALUES ('$first_name' , $last_name')");

? because PHP also is of the end that serves as group with semicolon,this is,The parse that additional semicolon can let PHP sometimes implement do not clear,What still drop elliptically so is good.Below this kind of circumstance,Although omited semicolon,But the meeting when PHP is carrying out MySQL to command helps you automatically be added.

The case that ? still has not to add branch additionally.The person that think a vertical stroke that wants a field when you is arranged show come down,is not to resemble normally in that way when sidewards is arranged,You can end sentence of a SQL with G,Do not use at this moment on semicolon,For example:

SELECT * FROM PENPALS WHERE USER_ID = 1G


2. TEXT, DATE, with type of SET data

The field that data of ? MySQL expresses must have a definition type of a data.This has about 25 is planted choice,Be direct palpability for the most part,Dispute of not much fee.But a few are necessary to be carried.

? TEXT is not type of a kind of data,Although the likelihood is on some books so those who say.It should be actually " LONG VARCHAR " or " MEDIUMTEXT " .

The format of type of data of ? DATE is YYYY-MM-DD,For instance:1999-12-08.You are OK the very easy current system time that will receive this kind of form with Date function:Date("Y-m-d" )

? and,Subtration can be made between type of DATA data,The time day that gets discrepant is counted:

$age = ($current_date - $birthdate);

? gather SET is an useful data type,It and enumerate ENUM are a bit similar,Just be SET can save many costs and ENUM can save a cost only just.And,SET type can have 64 only at most booked value,And ENUM type can be handled however most 65, 535 the value that defines beforehand.And if need to have,be more than 64 the gather of the value,How should do?Define many gather to solved this problem together with respect to need at this moment.

3. Match accord with

Of ? SQL match accord with to two is plant:" * " and " % " .Use below different situation respectively.For example:If you want to see all content of the database,Can resemble such coming that inquire:

SELECT * FROM Dbname WHERE USER_ID LIKE '%';

Here,Two are connected matched accord with to be used.Do they express identical meaning? ? It is to be used match any string,But they are used in different context." * " with will match field name,And " % " with will match a field to be worth.Another is not easy the place of bring to sb's attention is " % " connect need matching accord with and LIKE key word to be used together.

Still one is connected match accord with,It is underline " _ " ,The meaning of its representing and above different,It is to use those who match any single character.


4. NOT NULL and sky are recorded

If ? pressed Submit button below the case that the user was not writing pron any thing,Meeting how?If you need a value really,Can carry script with the client so or server end foot undertakes data test and verify originally,This had said in front.But,Be to allow a few fields to be come out for nothing however in the database whats are filled.To this kind of record,MySQL will carry out a few things for it:Insert value NULL,This is default operate.
If you do it in field definition,sound understands NOT NULL (when be being built or revising this field) ,MySQL will come out this field sky what thing also is not filled.To the field of type of enumerate of an ENUM,If you do it,sound understands NOT NULL,MySQL will insert the first value of enumerate collect in the field.That is to say,The default that MySQL regards this enumerate type as the first value of enumerate collect is worth.

? is worth the record that is NULL and an empty record is those who have a few distinction.% is connected match accord with to be able to match empty record,But cannot match NULL record however.In certain moment,The consequence that this kind of distinction can cause a few expect to be less than.With respect to my experience character,Any fields should state for NOT NULL.Statements of such SELECT inquiry below can run normally:

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

$selectresult = Mysql_query ("SELECT * FROM Dbname
' of willow of WHERE FIRST_NAME = '
AND LAST_NAME = ' is like wind '
AND CITY LIKE '$CITY'
") ;

? is in group,If the user did not appoint a CITY,be worth,Can use a % matching accord with to take the place of so into CITY variable,Meet when such searches any CITY value considers to go in,Include those CITY fields to be empty record even.

? but if have a few records,Value of its CITY field is NULL,At this moment the problem appeared.The inquiry above is cannot discover these fields quite.One of the problem settles way can be such:

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

$selectresult = Mysql_query ("SELECT * FROM Dbname
' of willow of WHERE FIRST_NAME = '
AND LAST_NAME = ' is like wind '
AND (CITY LIKE '$CITY' OR CITY IS NULL)
") ;

When ? notices to searching NULL,Must use " IS " key word,And won't work normally when LIKE.

What ? should mention finally is,Before if you are in,revising a new field,There had been a few records in the database,The field that this stylish joins is worth mediumly in original record,The likelihood is NULL,It is possibly also empty.A Bug that this also is MySQL,Fall in this kind of circumstance so,Inquiry of use SELECT wants special caution.

Related:

    收藏到网摘:

    Submit: