| <?php | 
 |  | 
 | include_once "User.php"; | 
 | include_once "config.php"; | 
 |  | 
 | use \User as User; | 
 |  | 
 | function open_db() { | 
 |     global $DB, $DB_USER, $DB_PASS; | 
 |     try { | 
 |         # MS SQL Server and Sybase with PDO_DBLIB  | 
 |         $DBH = new PDO($DB, $DB_USER, $DB_PASS); | 
 |     } | 
 |     catch(PDOException $e) { | 
 |         echo $e->getMessage(); | 
 |     } | 
 |     return $DBH; | 
 | } | 
 |  | 
 | function db_add_user(User $user) | 
 | { | 
 |     $DBH = open_db(); | 
 |     $columns = getColumnNames($DBH, "person"); | 
 |     foreach ((array) $user as $key => $value) { | 
 |         if (array_key_exists($key, (array) $columns)) { | 
 |             $a[$key] = $value; | 
 |         } | 
 |     } | 
 |     $keys = array_keys($a); | 
 |     $sql = "INSERT INTO person (".implode(", ",$keys).") \n"; | 
 |     $sql .= "VALUES ( :".implode(", :",$keys).")"; | 
 |     $q = $DBH->prepare($sql); | 
 |     $i=1; | 
 |     foreach($a as $value) | 
 |         $q->bindParam($i++, $value); | 
 |     print $sql. "\n"; | 
 |     return $q->execute($a); | 
 | } | 
 |  | 
 | function getColumnNames($dbh, $table) | 
 | { | 
 |     $sql = "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = :table"; | 
 |     try { | 
 |         $stmt = $dbh->prepare($sql); | 
 |         $stmt->bindValue(':table', $table, PDO::PARAM_STR); | 
 |         $stmt->execute(); | 
 |         $output = array(); | 
 |         while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { | 
 |             $output[$row['COLUMN_NAME']] = $row['COLUMN_NAME']; | 
 |         } | 
 |         return $output; | 
 |     } catch (PDOException $pe) { | 
 |         trigger_error('Could not connect to MySQL database. ' . $pe->getMessage(), E_USER_ERROR); | 
 |     } | 
 | } | 
 |  | 
 | try { | 
 |     # MS SQL Server and Sybase with PDO_DBLIB  | 
 |     $DBH = new PDO($DB, $DB_USER, $DB_PASS); | 
 |     $table = "person"; | 
 |     $sql = "SHOW COLUMNS FROM " . $table; | 
 |     $stmt = $DBH->prepare($sql); | 
 |     # $stmt->bindValue(':table', $table, PDO::PARAM_STR); | 
 |     $stmt->execute(); | 
 |     $output = array(); | 
 |     while($row = $stmt->fetch(PDO::FETCH_NUM)){ | 
 |         $output[] = $row[0]; | 
 |         print $row[0] . "\n"; | 
 |         } | 
 |     } | 
 |   catch(PDOException $e) { | 
 |       echo $e->getMessage(); | 
 |   } | 
 |  | 
 |   $user=new User(); | 
 |   $user->username="test"; | 
 |   print $user->to_string(); | 
 |   db_add_user($user); |