本文实例讲述了PHP使用mysql与mysqli连接Mysql数据库的方法。分享给大家供大家参考,具体如下:
代码很简单直接上了
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
<?php /** * @Author: HTL * @Description: Description */ // 降低PHP默认的错误级别 // 只显示除禁用以外的所有错误 // 解决因为PHP5.3+版本太高而导致在使用mysql_connect时出现的弃用警告“Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead” error_reporting (E_ALL ^ E_DEPRECATED); $db_host = "localhost" ; $db_user = "root" ; $db_passwd = "" ; $db_name = "mysql" ; echo ( "<BR>---------------------------mysql_connect------------------------<BR><BR>" ); $query = "SELECT SESSION_USER(), CURRENT_USER(),now();" ; $conn = mysql_connect( $db_host , $db_user , $db_passwd ); if (! $conn ) { die ( 'Could not connect: ' . mysql_error()); } mysql_select_db( $db_name , $conn ); $result = mysql_query( $query ); while ( $row = mysql_fetch_array( $result )){ var_dump( $row ); } mysql_close( $conn ); echo ( "<BR>---------------------------mysqli_connect------------------------<BR><BR>" ); $conn = mysqli_connect( $db_host , $db_user , $db_passwd , $db_name ) ; if (! $conn ) { die ( 'Could not connect: ' . mysqli_error()); } //execute the query. $result = mysqli_query( $conn , $query ); //display information: while ( $row = mysqli_fetch_array( $result )) { var_dump( $row ); } mysqli_close( $conn ); exit (); ?> |
有图有真相
希望本文所述对大家PHP程序设计有所帮助。