Home > Backend Development > PHP Tutorial > Simple example of php connecting to database

Simple example of php connecting to database

WBOY
Release: 2016-07-25 08:46:38
Original
1116 people have browsed it
Friends who have just learned PHP often don’t know how to connect to the database. This article gives a simple example to help you easily create a database connection. Friends in need, come and take a look.

Demo code:

<?php
/**
 * php 连接数据库
 * site bbs.it-home.org
**/
 $mydbhost  ="localhost";  //配置主机  
 $mydbuser  ="root";   //数据库用户  
 $mydbpw   ="123456"; //数据库密码  
 $mydbname  ="testdb";  //数据库名  
   
 //$mydbcharset ="GBK";  
   
 $conn=mysql_connect($mydbhost,$mydbuser,$mydbpw) or die ("服务器连接错误".mysql_error());  
 mysql_select_db($mydbname,$conn);  
 mysql_query("SET NAMES 'utf8'");  
 date_default_timezone_set ('PRC');   
 $addtime=date("y-m-d H:i:s");  
   
 //定义常量  
 define('ALL_PS',"nkfwe24tot");//二次加密,密码字符  
 //echo md5("admin".ALL_PS);  
   
 //信息提示函数  
 function showmsg($text,$urlstr="")  
 {  
  if($urlstr){  
   echo '';  
   exit;  
  }  
  else {  
   echo "";  
   exit;  
  }   
}  
?>
Copy after login
If the above code is already a piece of cake for you, then you can read: php mysql database operation class php database operation class (implementing table additions, deletions, modifications, querying, fetching rows, querying multiple pieces of data, etc.) A simple mysql database class Refer to how others write database operation classes, and try to encapsulate a PHP database operation class yourself.


source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template