在wamp目录下的www下创建一个后缀名为php的记事本文件

输入以下代码 (推荐学习:PHP视频教程)
<?php
$con=mysql_connect("localhost:3306","用户名(默认root)","密码");
if(mysql_error())
{
echo"数据库连接失败";
die();//终止运行
}
mysql_select_db("数据库名",$con);
if(mysql_errno())
{
echo"数据库连接失败";
die();
}
echo"数据库连接成功";
?>要写sql语句的话
例如:
立即学习“PHP免费学习笔记(深入)”;
<?php
$con=mysql_connect("localhost:3306","root","root")
or die("数据库连接失败<br>");
mysql_select_db("mysql_test",$con) or die("数据库连接失败!<br>");
mysql_query("set names 'gbk'");//设置中文字符
$sql = "insert into customers(cust_id,cust_name,cust_sex,cust_address,cust_contact)";
$sql=$sql."values(null,'李中华','0','深圳市','南山区')";
if(mysql_query($sql,$con)){
echo"数据库连接成功<br>";
}
else{
echo"数据库连接失败<br>";
}
?>以上就是php如何进入数据库的详细内容,更多请关注php中文网其它相关文章!
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号