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.