1.最简单的方式
1 | <span style= "font-family:'微软雅黑', 'Microsoft YaHei';font-size:14px;color:rgb(0,0,0);" ><?php<br> $con = mysql_connect( "localhost" , "root" , "" );<br> if (! $con )<br> {<br> die ( 'Could not connect: ' . mysql_error());<br> }<br>mysql_close( $con );<br>?><br></span> |
2.面向对象mysqli
1 | <span style= "font-family:'微软雅黑', 'Microsoft YaHei';font-size:14px;color:rgb(0,0,0);" ><?php<br> $db = new PDO( 'mysql:host=localhost;dbname=test' , 'root' , '' );<br> try {<br> foreach ( $db ->query( 'select * from user' ) as $row ){<br> print_r( $row );<br> }<br> $db = null; //关闭数据库<br>} <br> catch (PDOException $e) {echo $e->getMessage();<br>}<br>?><br></span> |
3.pdo连接mysql
1 | <span style= "font-family:'微软雅黑', 'Microsoft YaHei';font-size:14px;color:rgb(0,0,0);" ><?php<br> $db = new PDO( 'mysql:host=localhost;dbname=test' , 'root' , '' );<br> try {<br> foreach ( $db ->query( 'select * from user' ) as $row ){<br> print_r( $row );<br> }<br> $db = null; //关闭数据库<br>} <br>catch (PDOException $e) {<br> echo $e->getMessage();<br>}<br>?></span> |
4.ADODB连接mysql
1 | <span style= "font-family:'微软雅黑', 'Microsoft YaHei';font-size:14px;color:rgb(0,0,0);" ><?php<br> require_once './adodb5/adodb.inc.php' ;<br> $conn = &ADONewConnection( 'mysql' );<br> $conn ->connect( 'localhost' , 'root' , '' , 'test' );<br> $conn ->Execute( "set names utf8" );<br> $res = $conn ->Execute( "select * from user" );<br> if (! $res ){<br> echo $conn ->ErrorMsg();<br>} else {<br> var_dump( $res );<br>}<br>?></span> |