<?php // 1.connect mysal try { $a; // null $b = is_int ( 2 ); // bool $c = 2; // int $d = 2.5; // float $str = "abc"; // string; $arr = array ( "username" => "user1", "pwd" => 123456 ); // array(); $pdo = new PDO ( "mysql:host=localhost;dbname=stuDemo", "root", "" ); // object $file = fopen ( "./data.txt", "w+" ); // resource $dsn = "mysql:host=localhost;dbname=stuDemo"; $name = "root"; $pwd = ""; $pdo = new PDO ( $dsn, $name, $pwd ); // object $pdo -> setAttribute ( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION ); // bool } catch ( PDOException $e ) { // $e只是一个变量而已,可以改成$a,$b $e -> getMessage (); } // 2.write sql $sql = "SELECT * FROM user"; // string // 3.execute sql $stmt = $pdo -> query ( $sql ); // object 通过$pdo -> query($sql)方法,得到object(PDOStatement类) $row = $stmt -> rowCount (); // int $res = $stmt -> fetch ( PDO::FETCH_ASSOC ); //array $res, result is an array();
时间: 2024-10-25 05:39:22