本教程讲了三个实例都是关于php操作mysql数据库的教程,一是连接mysql数据库,二是查询数据库字段名字,三是查询数据库记录.
连接mysql数据库,代码如下:
- $link = mysql_connect("localhost", "mysql_user", "mysql_password")
- or die("could not connect: " . mysql_error());
- print ("connected successfully");
- mysql_close($link);
查询mysql字段名,代码如下:
- $link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
- $fields = mysql_list_fields("database1", "table1", $link);
- $columns = mysql_num_fields($fields);
- for ($i = 0; $i < $columns; $i++) {
- echo mysql_field_name($fields, $i) . " ";
- }
查询数据,代码如下:
- $conn=mysql_connect("localhost","phpdb","phpdb")
- or die("不能连接数据库服务器: ".mysql_error());
- mysql_select_db("test",$conn) or die ("不能选择数据库: ".mysql_error());
- $result = mysql_query("select * from user",$conn);
- while($row=mysql_fetch_array($result)){
- print "name:".$row[1];
- print " address:".$row[3];
- print " tel:".$row[4];
- print " email:".$row[5];
- echo
- "<br>";
- }
|