---
title: "php连接mysql数据库实例"
author: "cainwise"
date: "2008-11-29T14:25:25Z"
tags: []
slug: "php-mysql"
draft: false
lastmod: "2008-11-29T14:25:25Z"
id: 282867
url: "https://cainwise.iteye.com/blog/282867"
---

```
<?php
 header("Content-Type: text/html; charset=GBK");
 echo "你好<br>";
 $conn = @mysql_connect("localhost","root","root");//连接数据库   
 mysql_select_db("test",$conn);//选择数据库   
 if($conn){
	echo "连接成功<br>";
	$sql = "insert into test(users) values('mars')";
	mysql_query("SET NAMES 'GBK'");//处理乱码 
    mysql_query($sql,$conn);
	echo "插入条数为";

	$recode="select users from test";
	$result=mysql_query($recode,$conn);
	$re_num = mysql_num_rows($result);//获取记录数   
    echo $re_num; 
	echo "<table border=1><tr><td>用户名</td></tr>";
    while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    	$users=$row["users"];
    	echo "<tr><td>".$users."</td></tr>";
    } 
	echo "</table>";
 }else{
	echo "连接失败";
 }
 ?>
```