问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501

php做的网站 连接mysql数据库 效率问题

发布网友 发布时间:2022-04-07 23:56

我来回答

4个回答

热心网友 时间:2022-04-08 01:25

你可以把连接的方法写进类里,让它形成方法比如
class something {

global $db;
function web_db(){
$this->db = $this->database(); //把连接方法存如属性里
}
function database($server = 'localhost',$root = 'root',$pass = '****'){
......//这里写你的连接方法,及其关联表操作
}
...//其他方法
} //class end
在你的页面程序中这样写
require_once "web_common.class.php";//包进你的类文件
$mysql = new something;
在你需要数据查询或写入的时候只要调用 $mysql->db;就好了

如上你可以看出,无论你有多少客户请求数据库,而数据库只在载入页面时连接一次而已,调用 $mysql->db 只是请求程序,请求方法而已,没有请求数据库。第一,数据库连接查询只有一次;第二无形中也加快了页面的载入速度。
记住,你做网页不是给一个人两个人用的,而是很很很多个,为了保证数据库的正常使用,在多请求的情况下依然能很好工作,这是个很好的办法。
最后close是可写可不写的,因为当mysql没有请求时,它会自动关闭。

热心网友 时间:2022-04-08 02:43

关注

热心网友 时间:2022-04-08 04:18

如果想要优化可以 把数据库查询结果缓存在内存中,可以直接调用

当然使用数据库永久连接mysql_pconnect()不晓得能不能达到你那个目的 呵呵

热心网友 时间:2022-04-08 06:09

PHP in the database to support the rich, it is also one of the reasons why quickly became popular, it supports the following databases or data files:
Adabas D
DBA
DBase
Dbm
FilePro
Informix
InterBase
MSQL
Microsoft SQL Server
MySQL
Solid
Sybase
ODBC
Oracle 8
Oracle
PostgreSQL
In the Internet, it also supports a significant number of communication protocols (protocol), including relevant e-mail with IMAP, POP3; network management system SNMP; network news NNTP; account shared NIS; global information network and HTTP server Apache; directory LDAP protocol and other network-related functions.
In addition, written in PHP using the Web back-end CGI program can be easily ported to the different operating systems. For example, the first plane to Linux's Web site, the system load is too high, they can quickly move the entire system on SUN workstations, do not have to recompile CGI program. In the face of the rapid development of the Internet, this is the best long-term planning.
Variable type:
PHP There are a variety of variables; these are:
- Digital (integer - example: 32)
- Boolean (boolean - cases: TRUE)
- String (string - case of: 'a string of text')
- NULL
- Resources (resource)
- Array (array - example: arrayname [2])
Syntax:
There are three kinds of grammar:
/ / Comment
/ * Comment * /
# Comment
The basic "Control Structures":
* If ... else
If (condition == true);
* If ... else then
If (condition == true)
Else if (condition2 == true);
A PHP example:
<html>
<head>
<title> First program </ title>
</ Head>
<body>
<? Php
Echo "hello world";
?>
</ Body>
</ Html>
Take a look:
Php official website: www.php.net
Php support for object-oriented
The concept of object-oriented programming:
The argument between the different authors may not be the same, but there must be a OOP language in the following areas:
Abstract data types and information package
Succession
Polymorphism
PHP is in the category to complete the package:
<? Php
Class Something (
/ / OOP in the category, usually the first characters to uppercase
Var $ x;
Function setX ($ v) (
/ / Lower-case approach for the beginning of words, and then use capital letters to separate words, such as getValueOfArea ()
$ This-> x = $ v;
)
Function getX () (
Return $ this-> x;
)
)
Of course, you can define their own preferences, but the best to maintain a standard would be more effective. Members of the data used in the category of "var" to define the statement, addressed to the members of the data prior to the assignment, they are not the type. A member of the data can be a whole number, an array, an array of related (associative array) or an object. In the category method is defined in the form of a function in ways to access the members of the class of variables, you should use $ this-> name, or a method, it is the only local variables.
The use of new operators to create an object:
$ Obj = new Something;
Then you can use a member function through:
$ Obj-> setX (5);
$ See = $ obj-> getX ();
In this case, setX members of the function will be assigned to the target of 5 members of the variable x (not the kind of), and then return to getX its value 5. As can be: $ obj-> x = 6 through the categories cited as ways to access data members, this is not a good habit of OOP. I strongly recommend the adoption of methods to access the members of the variables. If you can not be seen as members of the variables to deal with and handle only object to the use of methods, you will be a good OOP programmers. Unfortunately, PHP does not support the statement of the members of the private variable, so bad code in PHP is also permitted. In succession in PHP is easy to achieve, as long as extend the use of keywords.
<? Php
Class Another extends Something (
Var $ y;
Function setY ($ v) (
$ This-> y = $ v;
)
Function getY () (
Return $ this-> y;
)
)
"Another" category of objects now has a parent (Something) of all the members of the data and methods, but also add their own data members and methods.
You can use the
$ Obj2 = new Something;
$ Obj2-> setX (6);
$ Obj2-> setY (7);
PHP is not support multiple inheritance, so you can not two or more types of derivatives to a new category. You can re-definition of derived classes in a way, if we are in the "Another" category in the re-definition of getX, we will not be able to use the "Something" in the getX methods. If you're in a statement in the derived class with a data base to send members of the same name, so when you deal with it, it will "hide" members of the data base class.
You can define your class constructor. Constructor is a category with the name of the method of the same name, when you create a category of object would be called, for example:
<? Php
Class Something (
Var $ x;
Function Something ($ y) (
$ This-> x = $ y;
)
Function setX ($ v) (
$ This-> x = $ v;
)
Function getX () (
Return $ this-> x;
)
)
So you can create an object through:
$ Obj = new Something (6);
Constructor will be automatically assigned to the 6 variable data x. Constructor and methods are common PHP function, so you can use the default parameters.
Function Something ($ x = "3", $ y = "5")
Then:
$ Obj = new Something (); / / x = 3 and y = 5
$ Obj = new Something (8); / / x = 8 and y = 5
$ Obj = new Something (8,9); / / x = 8 and y = 9
Default parameters of the use of C + + way, so you can not ignore the value of Y, and X to a default parameters of the assignment is from left to right, if the incoming parameter is less than the requirements of parameters, will make its The use of default parameters.
When a derived class of object is created, only its constructor is called, the constructor of the class of the father was not called, if you want to call the base class constructor, you must be in the derived class constructor call in show. To do so because the derived class father of all types of methods are available.
<? Php
Function Another () (
$ This-> y = 5;
$ This-> Something ();
/ / Display base class constructor call
)
The OOP is a very good mechanism for the use of abstract categories. Is not an abstract example, can only be made available to a derived class interface. Designers often use the abstract class to force programmers derived from the base class, so as to ensure that the new category includes a number of look forward to. PHP is not in the standard way, but: If you need the characteristics of the base class can be defined, and its constructor add "die" call, so that we can not guarantee that the base is an example of the now In each of the methods (interface) followed by "die" statement, so that if a programmer in the derived class does not cover method will lead to a mistake. And because PHP is not the type, you may need to recognize an object from your base class in the derived class, then added a category-based approach to the real identity of the type of justice (to return to some kind of identification id), and You receive the parameters of an object when the value of the check. Of course, if an evil bad programmers in the derived class in the coverage of this method does not work on, but the general problems found in the lazy programmers, and not on the evil programmer.
Of course, allows programmers to the base class can not see is a very good, as long as the interface to print out their work done on it. PHP is not in the destructor.
Heavy (with a different cover) in PHP does not support. In OOP, you can overload a way to achieve two or more methods with the same name, but a different number or type of parameters (which depends on the language). PHP is a loose type of language, so through the type of heavy-ty work, but through a number of different parameters to heavy also does not work.
OOP in heavy at times in the constructor is very good, so you can create objects in different ways (passing a different number of parameters). In PHP to achieve its techniques are:
<? Php
Class Myclass (
Function Myclass () (
$ Name = "Myclass". Func_num_args ();
$ This-> $ name ();
/ / Note that $ this-> name () are wrong, but here is a $ name will be called the method name
)
Function Myclass1 ($ x) (
Code;
)
Function Myclass2 ($ x, $ y) (
Code;
)
)
By category of additional processing, the use of this category is transparent to the user:
$ obj1 = new Myclass ('1 '); / / will call Myclass1
$ obj2 = new Myclass ('1 ','2'); / / will call Myclass2
This is sometimes very easy to use.
声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com
说课包括哪些方面 说课内容包括()。 如何在手机百度上删除对话记录? 结核病是什么样的疾病? 曹丕17岁得了肺痨,明知自己命不长久,还要强争王位,是不是很自私呢?_百... 古代小说常出现的病名 急求一篇"生活小窍门"(500字)的作文 至今最有什么小妙招 健康的戒烟方法 笔记本电池锁死是什么原因引起的? 长相丑陋是什么动物 请问十二生肖哪个动物长的最丑? 十二生肖,最丑的动物 十二生肖有哪些披头散发丑陋不堪的动物 最丑的动物是什么 长相丑陋的动物是什么吘? 世界上最丑的动物是什么呢? 世界上什么动物最丑? 什么动物最丑 森林里有哪些动物很丑? 微信聊天记录会占用存储空间吗? 网站文字颜色代码应该怎么写 html中怎样去定义颜色?就是颜色的代码是哪些? 快手视频发了三小时还能上热门吗? 如何获取网页文字颜色 改变颜色 网页中foo,表示什么颜色 删除微信私聊消息能减少内存吗?就是不在通用那里删,直接清空消息那里删... 网页制作的字体颜色的代码谁有啊 HTML字体颜色代码怎么获取 请问 ,微信长按对话框“删除该聊天”,聊天记录就删没了,那还占用手机存储吗? 十二生肖里最丑的动物是什么 单线程和多线程的区别 单线程是什么意思 多线程和单线程是什么意思啊? 单线程和多线程的区别 什么是单线程、多线程? 在java中单线程和多线程是什么意思,他们有什么区别,分别的作用是什么? 什么是Java单线程啊? 为什么魅族pro6 plus的安兔兔跑分才11000多点,跑分、单线程、多线程分别是什么意思? 人类的大脑是“多线程”还是“单线程”的呢 中国新声代第三季的第3季歌单·毕业盛典 多线程是什么意思? 使用迅雷下载中的单线程,多线程是什么意思? 为什么我的思考和记忆都是单线程? CPU单线程和双线程是什么意思? 中国新声代第三季报名的网址(官方网站) 怎样不在微信头像栏显示自己的手机号 怎样才能把手机号放在微信头像照片中 怎么改成手机号? 华为U7PRO怎么在华为智慧屏上设置混响 怎样从youTube上拖视频?希望得到详细的方法,谢谢