Create a notepad file with the suffix name PHP under the WWW in the wamp directory
Enter the following code (Recommended learning: PHP video tutorial)
<?php $con=mysql_connect("localhost:3306","用户名(默认root)","密码"); if(mysql_error()) { echo"数据库连接失败"; die();//终止运行 } mysql_select_db("数据库名",$con); if(mysql_errno()) { echo"数据库连接失败"; die(); } echo"数据库连接成功"; ?>
If you want to write sql statements
For example:
<?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>"; } ?>
The above is the detailed content of How to enter the database in php. For more information, please follow other related articles on the PHP Chinese website!