将PDO与准备好的语句一起使用(我从这里学到:http://www.kitebird.com/articles/php-pdo.html)我正在使用一个打开数据库连接的函数:
function testdb_connect ()
{
$dbh = new PDO("mysql:host=localhost;dbname=test", "testuser", "testpass");
return ($dbh);
}
Run Code Online (Sandbox Code Playgroud)
我有一个for循环,迭代并在表中插入行.我应该在循环之前打开连接并在循环结构之后关闭它
$dbh = testdb_connect();
for($i=0; $i<$number_of_values; $i++){
//Insert rows
}
$dbh = NULL ;
Run Code Online (Sandbox Code Playgroud)
或者我应该在每个插入语句的循环内打开和关闭它?每种方法的优缺点是什么?
for($i=0; $i<$number_of_values; $i++){
$dbh = testdb_connect();
//Insert rows
$dbh = NULL ;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1751 次 |
| 最近记录: |