<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无标题文档</title> </head> <body> <?php //造DSN:驱动名:dbname=数据库名;host=服务器地址 $dsn="mysql:dbname=mydb;host=localhost"; $pdo=new PDO($dsn,"root",""); //写sql语句v $sql="select * from Info"; //$v=$pdo->query(); //var_dump($v); //foreach($v as $a) //{ // echo $a[0]; //} //预处理sql语句,先把sql语句放到服务器上等待我们给他指令去执行 $r=$pdo->query(); var_dump($r); $stm=$pdo->prepare($sql);//准备一条sql语句 //执行SQL语句,执行成功返回true,失败返回false if($stm->execute()) { var_dump($stm->fetch());//1.逐行取数据 //var_dump($stm->fetchAll());//2.取所有的 //$stm->fetchColumn(1);//3.取某一列 //var_dump($stm->fetchObject());//4.返回对象 } else {echo "执行失败!";} ?> </body> </html>
时间: 2024-10-20 14:37:45