Home  >  Article  >  Backend Development  >  Simple example of php connecting to database

Simple example of php connecting to database

WBOY
WBOYOriginal
2016-07-25 08:46:381043browse
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;  
  }   
}  
?>
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.


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