标签:
pdo:php data object
//查看开启的方法 //echo phpinfo(); //单例 class A{ private static $a = null; static function shareInstance() { if(self::$a == null) { self::$a = new self(); } return self::$a; } }
.
<?php /** * Created by PhpStorm. * User: forr * Date: 15/9/20 * Time: 12:23 */ //连接数据库 try{ //$pdo = new PDO("mysql:host=localhost;dbname=forrdb;");//明文 // $pdo = new PDO("uri:xxxx.ini");//读资源xxxx.ini配置文件 $pdo = new PDO("mysqlpod");//在php.ini文件中配置dns }catch(PODException $error){ die("数据库连接失败".$error->getMessage()); } // 执行查询 $sql = "select * from stu"; $stmt = $pdo->query($sql); $list = $stmt->fetchAll(POD::FETCH_ASSOC);//返回带key的字典,非下标的 print_r($list); //释放资源 $stmt = null; $pdo = null; //快捷查询 foreach($pod->query($sql) as $val) { echo $val[‘id‘]."<br>"; } //增加一行数据 $sql = "insert into stu values(null,‘boy‘,‘man‘,‘18‘)"; //删除行 $sql = "delete from stu where id = 6"; // 修改行 $sql = "update stu set name = ‘jobs‘ where id = 3"; $result = $pdo->exec($sql); if($result) { echo "成功执行"; }
标签:
原文地址:http://www.cnblogs.com/huen/p/4823196.html