Skip to content

Instantly share code, notes, and snippets.

@zazk
Created June 6, 2014 20:16
Show Gist options
  • Save zazk/753d44c1d156e6123c0d to your computer and use it in GitHub Desktop.
Save zazk/753d44c1d156e6123c0d to your computer and use it in GitHub Desktop.
Execute an SQL Query with PDO - PHP
<?php
//Execute an SQL Query with PDO
$table = "company";
try {
$db = new PDO("mysql:dbname=socialpi_openmobile;host=localhost", "socialpi_opmo", "openmo" );
$db->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );//Error Handling
$sql ="
DROP TABLE IF EXISTS `contacto`;
CREATE TABLE IF NOT EXISTS `contacto` (
`contactoid` int(5) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL DEFAULT '',
`company` varchar(255) DEFAULT '',
`email` varchar(255) NOT NULL DEFAULT '',
`message` text,
`date_created` date DEFAULT '0000-00-00',
`first` varchar(55) NOT NULL,
`second` varchar(55) NOT NULL,
`third` varchar(55) NOT NULL,
`fourth` varchar(55) NOT NULL,
PRIMARY KEY (`contactoid`)
) ENGINE=MyISAM ;
" ;
$db->exec($sql);
print("Created $table Table.\n");
} catch(PDOException $e) {
echo $e->getMessage();//Remove in production code
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment