数据库连接初始化
123456789101112131415161718192021 | <?php try { $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 = '数据库连接失败!' ; $ee = $e ->getMessage(); echo $error ; echo '</br>' ; echo $ee ; exit (); } $error = '数据库连接成功!' ; echo $error ; $pdo = null; ?> |
向数据库发送内容更新并获取返回值
123456789101112131415 | <?php try { $sql = 'UPDATE joke SET jokedate="2012-04-01" WHERE joketext LIKE "%chicken%"' ; $affectedRows = $pdo -> exec ( $sql ); } catch (PDOException $f ) { $dd = '内容更新错误: ' . $f ->getMessage(); echo $dd ; exit (); } $dd = "更新了 $affectedRows 行。" ; echo $dd ; ?> |
向数据库获取内容(失败,尝试中ing。。。)
12345678910111213141516 | <?php try { $sql1 = 'SELECT joketext FROM joke' ; $result = $pdo ->query( $sql1 ); } catch (PDOException $g ) { $ff = '查询失败: ' . $g ->getMessage(); echo $ff ; exit (); } echo $result [ 'joketext' ]; ?> |