Database connection Initialization
<?phptry {$pdo = new PDO (' Mysql:host=localhost;dbname=test ', ' testuser ', ' Pass '), $pdo->setattribute (pdo::attr_ Errmode,pdo::errmode_exception); $pdo->exec (' SET NAMES ' utf8 '); } catch (Pdoexception $e) {$error = ' database connection failed! ‘; $ee = $e->getmessage (); Echo $error; Echo ' </br> '; Echo $ee; Exit (); } $error = ' database connection succeeded! '; echo $error; $pdo = null;? >
Send content updates to the database and get return values
<?phptry{$sql = ' UPDATE joke SET jokedate= ' 2012-04-01 ' WHERE joketext like '%chicken% '; $affectedRows = $pdo->exec ( $sql);} catch (Pdoexception $f) {$dd = ' content update error: '. $f->getmessage (); Echo $dd; exit ();} $DD = "Updated $affectedRows line. "; Echo $dd;? >
Get content to the database ( failed, try ing ... )
<?phptry{$sql 1 = ' SELECT joketext from joke '; $result = $pdo->query ($sql 1);} catch (Pdoexception $g) {$ff = ' query failed: '. $g->getmessage (); Echo $ff; exit ();} echo $result [' joketext '];? >
This article is from the "Cocoa sauce data collection Room" blog, reproduced please contact the author!
PHP Project notes (ii) PHP section