1 php 2 const DSN = "mysql:host=127.0.0.1:3306;dbname=test"; 3 const USER_NAME = "dba"; 4 const PASSWORD = "dba"; 5 6 try{ 7 //1.创建PDO对象 8 $pdo = new PDO(DSN,USER_NAME,PASSWORD); 9 //2.设置编码格式 10 $pdo->exec("SET names utf8"); 11 //3.设置错误使用的异常模式 12 $pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION); 13 14 }catch (PDOException $e){ 15 echo $e->getMessage(); 16 exit; 17 } 18 19 try{ 20 //使用PDO中的方法执行SQL语句 21 $sql = "select * from large_user_log"; 22 $rows = $pdo->query($sql); 23 while($row = $rows->fetch(\PDO::FETCH_ASSOC)){ 24 $data[] = $row['id']; 25 } 26 27 // $goods = $rows->fetchAll(PDO::FETCH_ASSOC); 28 29 //返回主键方法 30 var_dump($rows); 31 var_dump($data); 32 33 }catch (PDOException $e){ 34 echo "错误原因为:".$e->getMessage(); 35 exit; 36 }