php mysql连接例子
<?PHP @$conn = mysql_connect("127.0.0.1","root",""); //返回false或resource if(!$conn){ die(mysql_error()); } $db_selected = mysql_select_db("Route",$conn); //返回false或true if(!$db_selected){ die(mysql_error()); } //查找数据 $sql = "select * from r_product"; $re = mysql_query($sql); //返回resource或false if(!$re){ die(mysql_error()); } while($arr = mysql_fetch_array($re)){ //返回的数组包括索引数组和关联数组两种 var_dump($arr); } while($arr = mysql_fetch_row($re)){ //返回的数组包括索引数组 var_dump($arr); } while($arr = mysql_fetch_assoc($re)){ //返回的数组包括关联数组 var_dump($arr); } mysql_close($conn); //关闭数据库连接 ?>
PDO
$dsn = "mysql:host=127.0.0.1;dbname=test"; $db = new PDO($dsn, 'root', ''); $re = $db->query("select * from test limit 1"); while($a = $re->fetch()) { var_dump($a); }