php 如何在页面加载时调用php函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6106338/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to call the php function while the page is getting load
提问by Indu
I need to call the php function while the page is getting load.
我需要在页面加载时调用 php 函数。
Means when I click a link or menu of the page Homepage.php, then it will redirect to the page called History.php. Here before loading the page History.php, it sholud call the function to get the data from the database.
意思是当我点击页面 Homepage.php 的链接或菜单时,它将重定向到名为 History.php 的页面。这里在加载页面 History.php 之前,它应该调用函数从数据库中获取数据。
Please spend your valuable time to answer this query. Thanks in advance.
请花您宝贵的时间来回答这个问题。提前致谢。
回答by Quentin
Top of History.php:
History.php 的顶部:
<?php
include('/path/to/php/program/with/function.php');
the_function();
回答by dyasta
You are the one sending any response to the browser, so simply call whatever functions prior to sending the rest of the page (do at beginning of PHP page).
您是向浏览器发送任何响应的人,因此只需在发送页面的其余部分之前调用任何函数(在 PHP 页面的开头执行)。
回答by Naveed
You can also try at top of:
您也可以尝试在:
Hisory.php
历史.php
<?php
function getData(){
// function body
}
$data = getData();
?>
But if you are using this type of function on many places then you should place it in one file and include it in that file where you want to call this function. As Quentindescribed.
但是如果你在很多地方使用这种类型的函数,那么你应该把它放在一个文件中,并将它包含在你想调用这个函数的那个文件中。正如昆汀所描述的那样。
回答by inquam
The PHP runtime is an interpreter that will "automaticly" (unless configured otherwise) run .php
files. This mean that all code within <?php ?>
tags will get executed by the PHP engine. So you could place a php code block first in your page. Either directly containing the logic to fetch the data from you server.
PHP 运行时是一个解释器,它将“自动”(除非另有配置)运行.php
文件。这意味着<?php ?>
标签中的所有代码都将由 PHP 引擎执行。所以你可以先在你的页面中放置一个 php 代码块。要么直接包含从服务器获取数据的逻辑。
Example
例子
// Example code taken from php website
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
$query = sprintf("SELECT firstname, lastname, address, age FROM friends WHERE firstname='%s' AND lastname='%s'",
mysql_real_escape_string($firstname),
mysql_real_escape_string($lastname));
// Perform Query
$result = mysql_query($query);
// Check result
// This shows the actual query sent to MySQL, and the error. Useful for debugging.
if (!$result) {
$message = 'Invalid query: ' . mysql_error() . "\n";
$message .= 'Whole query: ' . $query;
die($message);
}
// Use result
// Attempting to print $result won't allow access to information in the resource
// One of the mysql result functions must be used
// See also mysql_result(), mysql_fetch_array(), mysql_fetch_row(), etc.
while ($row = mysql_fetch_assoc($result)) {
echo $row['firstname'];
echo $row['lastname'];
echo $row['address'];
echo $row['age'];
}
mysql_close($link);
If you want a persistent connection you use mysql_pconnect()
instead of mysql_connect()
.
如果您想要持久连接,请使用mysql_pconnect()
而不是mysql_connect()
.
I would however strongly encourage you to, at least, put database logic in a class which resides in a separate file which you include using include()
, include_once()
, require()
or require_once()
.
我不过强烈建议您,至少在驻留在一个单独的文件,你包括使用其中一类把数据库逻辑include()
,include_once()
,require()
或require_once()
。
If this is part of a bigger system and you have the time I would suggest you read up on the MVC pattern and look into a framework like Zend Framework or similar which have good database abstractions etc.
如果这是更大系统的一部分,并且您有时间,我建议您阅读 MVC 模式并研究像 Zend Framework 或类似的具有良好数据库抽象等的框架。
Good luck!
祝你好运!