fork download
  1. /*CREATE database test123 CHARACTER SET utf8 COLLATE utf8_general_ci;
  2. USE test123;
  3. CREATE TABLE test(
  4. id INT UNSIGNED AUTO_INCREMENT,
  5. name VARCHAR (50),
  6. mark INT UNSIGNED,
  7. PRIMARY KEY(id)
  8. );
  9. INSERT INTO test (name, mark) VALUES ('Иннокентий', 120), ('Афанасий', 190), ('Геннадий', 140);*/
  10.  
  11. <?php
  12. header('Content-type: text/html;charset=utf8');
  13. $pdo = new PDO('mysql:host=localhost;dbname=test123','root','');
  14. $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  15. $pdo->query("SET NAMES utf8");
  16. $sql = "SELECT id, name, mark FROM test ORDER BY :order LIMIT 0, 100";
  17. $stmt = $pdo->prepare($sql);
  18. $order = 'name';
  19. $stmt->bindValue(':order', $order, PDO::PARAM_STR);
  20. $stmt->execute();
  21. $result = $stmt->fetchAll();
  22.  
  23. echo '<table>';
  24. foreach($result as $row){
  25. echo '<tr>';
  26. echo "<td>{$row['id']}</td>";
  27. echo "<td>{$row['name']}</td>";
  28. echo "<td>{$row['mark']}</td>";
  29. echo '</tr>';
  30. }
  31. echo '</table>';
Runtime error #stdin #stdout #stderr 0.02s 24448KB
stdin
Standard input is empty
stdout
/*CREATE database test123 CHARACTER SET utf8 COLLATE utf8_general_ci;
USE test123;
CREATE TABLE test(
    id INT UNSIGNED AUTO_INCREMENT,
    name VARCHAR (50),
    mark INT UNSIGNED,
    PRIMARY KEY(id)
);
INSERT INTO test (name, mark) VALUES ('Иннокентий', 120), ('Афанасий', 190), ('Геннадий', 140);*/

stderr
PHP Warning:  Cannot modify header information - headers already sent by (output started at /home/oyMgx3/prog.php:11) in /home/oyMgx3/prog.php on line 12
PHP Fatal error:  Uncaught exception 'PDOException' with message 'could not find driver' in /home/oyMgx3/prog.php:13
Stack trace:
#0 /home/oyMgx3/prog.php(13): PDO->__construct('mysql:host=loca...', 'root', '')
#1 {main}
  thrown in /home/oyMgx3/prog.php on line 13