$DSN = "Mysql:host=localhost;dbname=test"; $username = ' root '; $password = ' 123456 '; $options = Array ( Pdo::mysql_attr_init_command => ' SET NAMES UTF8 ', ); $pdo = new PDO ($DSN, $username, $password, $options); $pdo->setattribute (Pdo::attr_errmode, pdo::errmode_exception); Increase: Method 1: Binding associative arrays $str = $pdo->prepare (INSERT into ' user ' (' username ', ' password ') VALUES (: username,:p assword) "); $str->execute (Array (": Username" => "test", ":p assword" => "passwd")); Method 2: Binding index Array $str = $pdo->prepare (INSERT into ' user ' (' username ', ' password ') VALUES (?,?) "); $str->execute (Array ("Test", "passwd")); By deleting: $str = $pdo->prepare ("Delete from user where ID > 3"); $str->execute (); Change: $str = $pdo->prepare ("UPDATE ' user ' SET username=:username,password=:p assword where Id=:id"); $str->execute (Array (": Username" => "test", ":p assword" => "passwd", ": id" => "3")); Check: Method 1: Single fetch, loop traversal, return to array $str = $pdo->prepare ("select * from user where ID >: ID ORDER by id"); $str->execute (Array (": id" =>2)); $str->setfetchmode (PDO::FETCH_ASSOC); Total three kinds: 1.pdo::fetch_both (default) 2.fetch_assoc 3.ftech_num
while ($data = $str->fetch ()) { Print_r ($data); Echo ' <br> '; } Method 2: All out, back to the two-dimensional array $str = $pdo->prepare ("SELECT * from User order by FID"); $str->execute (); $data = $str->fetchall (pdo::fetch_num); Print_r ($data); Method 3: Single fetch, loop traversal, bound field name to variable $str = $pdo->prepare ("Select Fid,username,password from User ID"); $str->execute ();
$str->bindcolumn ("id", $id); $str->bindcolumn ("username", $username); $str->bindcolumn (3, $password);
while ($str->fetch ()) { echo "$id | $username | $password <br> "; } echo "Total number of records:". $str->rowcount (). " <br> "; Echo Total number of fields:. $str->columncount (). " <br> "; |