Create a new connection for each request?

Note. I used google translator to write this

I always did the following for working with MySQL:

-> Open Connection to the database.
-> see details
-> Insert Data
-> another query
-> close Connection

I usually use the same connection to do different things before closing. A friend who studies this on Mexico's IPN told me that the right way (for security) is to create a new connection for each request, for example:

-> Open Connection to the database.
-> see details
-> close Connection
-> Open Connection to the database.
-> Insert Data
-> close Connection
-> Open Connection to the database.
-> another query
-> close Connection

My question is: what to do right? My method was to make the minimum number of database queries and only establish a connection and keep it until it no longer serves me.

Also, is it possible to do double insertion into a table? For instance:

insert into table1(relacion) values([insert into tablaRelacionada(id) values("dato")]);

and that "relacion" is the inserted identifier from the first request in "tablaRelacionada".

+3
2

- , . , :

1.

: .

, , , . , , .

2.

: , , PHP .

, , , "". , , . , MySQL , PHP, ( , , .. ], , , , MySQL.

, , .

3.

: . ? ... .

, - , , , . MySQL SSL, , .

 

TL; DR . . . , , .


, , , - :

$dbh->query("INSERT tablaRelacionada(id) values('dato')");
$lastid = $dbh->lastInsertId();
$dbh->query("INSERT INTO table1(relacion) values($lastid);");

, tablaRelacionada AUTO_INCREMENT, .

: lastInsertId()

+2

, INSERT. ( , INSERT... , INSERT .


, "" . "" , . . ( , , .)

- . " ", "", .

SQL, - "", .

.

, , , , . , , "". , (, , , , ), . script, , , , . ( , . , , , .)

+3

All Articles