If I execute some query with phpmyadmin it display the elapsed time 0.0002 sec, that’s 0.2 milliseconds.
If I execute the same query through PHP, using PDO, the elapsed time (just for the third line in the sample code below) is 20 milliseconds.
$db = new PDO('mysql:host=localhost;dbname=test', 'root', '', array(PDO::ATTR_PERSISTENT => true));
$cmd = $db->prepare($sql);
$cmd->execute(array($p1, $p2, $p3, $p4));
What’s wrong with this code?
Nothing at all, apart from the fact that you’re preparing the statement first, and then executing, which will take up (a bit) more time. That should not cause the fact that it’s a hundred times as slow though. Try to explain the query (EXPLAIN SELECT), and see what it’s doing. I find it hard to believe that using a prepared statement is that much slower.