Home  >  Article  >  Backend Development  >  How to enter the database in php

How to enter the database in php

(*-*)浩
(*-*)浩Original
2019-09-30 10:26:322136browse

Create a notepad file with the suffix name PHP under the WWW in the wamp directory

How to enter the database in php

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 &#39;gbk&#39;");//设置中文字符
$sql = "insert into customers(cust_id,cust_name,cust_sex,cust_address,cust_contact)";
$sql=$sql."values(null,&#39;李中华&#39;,&#39;0&#39;,&#39;深圳市&#39;,&#39;南山区&#39;)";
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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn