Preparing SQL Statements with PDO

后端 未结 4 1114
面向向阳花
面向向阳花 2021-01-15 12:59

My code looks like this:

// Connect to SQLite DB
DB(\'/path/to/sqlite.db\');

DB(\'BEGIN TRANSACTION;\');

// These loops are just examples.
for ($i = 1; $i          


        
4条回答
  •  情书的邮戳
    2021-01-15 13:16

    Why do you use prepared statements if you "prepare" them in the loop ? (in the DB function)

    Make a prepare befor the loop, and in the loop just give the values and execute. Of course this would require a rewrite of your DB method.

    Oh and btw. is your ID column the primary key ? if so you have another problem couse "i" would be for 100 "j" the same :)

    For example:

    $sth = $dbh->prepare('INSERT INTO "test" ("id", "name") VALUES (:id, :name)');
    $j=0;
    for ($i = 1; $i <= 10000; $i++){
       $j = ($j==100) ? 0 : $j++;
       $sth->execute(array(':id' => $i, ':name' => 'Testing ' . $j));     
    }
    

提交回复
热议问题