PHP连接mysql数据库

algorain

使用php连接mysql数据库,代码经过测试没有问题,需要改的是username,和password,databasename,也就是你数据库安装时的用户名和密码,还有你要连接的数据库名。localhost换成127.0.0.1应该也是可以的,下面的mysql_query("set names utf8;");使因为我的数据库有中文,我使用utf8编码,如果不加这句就会出现乱码,不是使用utf8编码的可以不加,至于报错提示die,也可以按照自己的需求改,这只是连接数据库和释放连接,下一篇再说php对mysql的具体操作。喜欢的可以收藏

1
2
3
4
5
6
7
8
9
10
11
12
<?php
$con = mysql_connect("localhost","username","password");
mysql_query("set names utf8;");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("databasename", $con);

mysql_close($con);
// some code
?>
  • Title: PHP连接mysql数据库
  • Author: algorain
  • Created at: 2017-02-03 10:47:53
  • Updated at: 2023-05-14 21:39:50
  • Link: http://www.rain1024.com/2017/02/03/php-article0010/
  • License: This work is licensed under CC BY-NC-SA 4.0.
 Comments
On this page
PHP连接mysql数据库