Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is a perfect opportunity for using <a href="http://docs.php.net/pdo.prepared-statements" rel="nofollow noreferrer">prepared statements</a>.<br> Also take a look at the tips at <a href="http://dev.mysql.com/doc/refman/5.0/en/insert-speed.html" rel="nofollow noreferrer">http://dev.mysql.com/doc/refman/5.0/en/insert-speed.html</a>, e.g. </p> <blockquote>To speed up INSERT operations that are performed with multiple statements for nontransactional tables, lock your tables</blockquote> <p>You can also decrease the number of queries. E.g. you can eliminate the <code>SELECT...FROM persons_table</code> to obtain the id by using <code>INSERT...ON DUPLICATE KEY UPDATE</code> and <a href="http://dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_last-insert-id" rel="nofollow noreferrer">LAST_INSERT_ID(<b>expr</b>)</a>. </p> <p>( sorry, running out of time for a lengthy description, but I wrote an example before noticing the time ;-) If this answer isn't downvoted too much I can hand it in later. )</p> <pre><code>class Foo { protected $persons_table='personsTemp'; protected $pdo; protected $stmts = array(); public function __construct($pdo) { $this-&gt;pdo = $pdo; $this-&gt;stmts['InsertPersons'] = $pdo-&gt;prepare(' INSERT INTO '.$this-&gt;persons_table.' (person) VALUES (:person) ON DUPLICATE KEY UPDATE id=LAST_INSERT_ID(id) '); } public function getActorId($name) { $this-&gt;stmts['InsertPersons']-&gt;execute(array(':person'=&gt;$name)); return $this-&gt;pdo-&gt;lastInsertId('id'); } } $pdo = new PDO("mysql:host=localhost;dbname=test", 'localonly', 'localonly'); $pdo-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // create a temporary/test table $pdo-&gt;exec('CREATE TEMPORARY TABLE personsTemp (id int auto_increment, person varchar(32), primary key(id), unique key idxPerson(person))'); // and fill in some data foreach(range('A', 'D') as $p) { $pdo-&gt;exec("INSERT INTO personsTemp (person) VALUES ('Person $p')"); } $foo = new Foo($pdo); foreach( array('Person A', 'Person C', 'Person Z', 'Person B', 'Person Y', 'Person A', 'Person Z', 'Person A') as $name) { echo $name, ' -&gt; ', $foo-&gt;getActorId($name), "\n"; } </code></pre> <p>prints</p> <pre><code>Person A -&gt; 1 Person C -&gt; 3 Person Z -&gt; 5 Person B -&gt; 2 Person Y -&gt; 6 Person A -&gt; 1 Person Z -&gt; 5 Person A -&gt; 1 </code></pre> <p>(someone might want to start a discussion whether a getXYZ() function should perform an INSERT or not ...but not me, not now....)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload