通过清心醉

PHP连接MYSQL

<?php
header(“Content-Type: text/html; charset=GBK”);
echo “连接数据库<br>”; //输出“连接数据库”并换行
$conn = @mysql_connect(“localhost”,”root”,”root”);

//连接数据库,地址为本机,MYSQL帐号密码同为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 “连接失败”;
}
?>

关于作者

清心醉 administrator

发表评论

请输入验证码: