# `PDO_MySQL`: Example
```
  <?php
  echo "PDO_MySQL Example" . "\n";

  try {
    $dsname = "mysql:host=127.0.0.1;port=4000;dbname=test";
    $dbhandle = new PDO($dsname, "root", "");
    print "Connection established.\n";
    ~!@$dbhandle->exec@!~("DROP TABLE IF EXISTS t_php");
    ~!@$dbhandle->exec@!~("CREATE TABLE t_php (id BIGINT PRIMARY KEY AUTO_RANDOM, name VARCHAR(20))");
    ~!@$num = $dbhandle->exec@!~("INSERT INTO t_php (name)"
      . " VALUES "
      . "('mysqli'), ('PDO_MySQL')");
    echo $num . " rows(s) inserted.\n";
    ~!@$statementhandle = $dbhandle->query@!~("SELECT id, name FROM t_php");
    while (~!@$row = $statementhandle->fetch@!~(PDO::FETCH_NUM)) {
      printf("ID: %s, Name: %s\n", $row[0], $row[1]);
    }
    print "Connection terminated.";
  } catch (PDOException $err) {
    die($err);
  }

  ?>
```

--------------------------------------------------------------
The program creates two rows with single INSERT statement.

The program uses the while construct to iterate through the rows in the result set, using the statement handle's `fetch()` method to return row data as an array of column values called `$row`. The `PDO::FETCH_NUM` style makes the individual column values in the array accessible via a numeric subscript.

Example:
```
<html>

<body>
  <h1>201.4-PHP Connection 13</h1>
  <?php
  echo "PDO_MySQL Example" . "<p>";

  try {
    $dsname = "mysql:host=127.0.0.1;port=4000;dbname=test";
    $dbhandle = new PDO($dsname, "root", "");
    print "Connection established.<p>";
    $dbhandle->exec("DROP TABLE IF EXISTS t_php");
    $dbhandle->exec("CREATE TABLE t_php (id BIGINT PRIMARY KEY AUTO_RANDOM, name VARCHAR(20))");
    $num = $dbhandle->exec("INSERT INTO t_php (name)"
      . " VALUES "
      . "('mysqli'), ('PDO_MySQL')");
    echo $num . " rows(s) inserted.<p>";
    $statementhandle = $dbhandle->query("SELECT id, name FROM t_php");
    while ($row = $statementhandle->fetch(PDO::FETCH_NUM)) {
      printf("ID: %s, Name: %s<p>", $row[0], $row[1]);
    }
    print "End.";
  } catch (PDOException $err) {
    die($err);
  }
  ?>

</body>

</html>
```
