<?php//Establish Connection object try { $pdo = new pdo (' mysql:host=localhost;dbname=test ', ' TestUser ', ' Pass '); $pdo->setattribute (pdo::attr_errmode,pdo::errmode_exception); //setattribute pdo object Methods, PDO:: Indicates that these variables are part of the PDO we use, not the PHP language itself built-in variables. You want to set the PDO property (Pdo::attr_errmode) of the control error mode to the pattern that throws the exception (Pdo::errmode_exception). $pdo->exec (' set names ' utf8 '); //the character code of the configuration database connection. } catch (pdoexception $e) { $output = ' Database connection Failed! '. $e->getmessage ();//Get a detailed error message from the MySQL server include ' output.html.php '; exit (); } $output = ' database connection was successful! ';include ' output.html.php ';//Build Form try{ $sql = ' create table joke ( id int not null auto_increment primary key, joketext text, jokedate date not null ) Default character set utf8 engine=innodb '; $pdo->exec ($sql); // Pass SQL query to Object}catch (pdoexception $e) {$output = ' table build failed: '. $e->getmessage (); include ' Output.html.php '; exit ();} $output = ' form success ';include ' output.html.php ';//update content try{$sql = ' update joke Set jokedate= "2012-04-01" WHERE joketext LIKE "%chicken%" '; $ad = $pdo->exec ($sql); //Gets the value returned, for delete,update and insert (which they want to modify to store the outgoing data), EXEC returns the number of rows in the affected table. }catch (pdoexception $e) { $output = ' content update error: '. $e GetMessage (); include ' output.html.php '; exit ();} $output = "Content update is successful, $ad line " is updated; //values have variables in double quotes. include ' output.html.php ';? >
This article is from the "Cocoa sauce data collection Room" blog, reproduced please contact the author!
PHP Learning Notes (ii) PHP connection with MySQL and send SQL query with PHP