8

I need to call the php function while the page is getting load.

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.

Please spend your valuable time to answer this query. Thanks in advance.

Indu
  • 149
  • 2
  • 4
  • 9
  • PHP will run anyway if you call History.php. Whatever code you need to run to fetch data, you will do in there. Why is that not sufficient? Or do you mean something else? – Pekka May 24 '11 at 06:06
  • Since PHP runs before the page is loaded, just call your function in history.php – JohnP May 24 '11 at 06:06
  • Yes @Pekka, but I need to call a function which should be executed before the History.php page get download(execute). – Indu May 24 '11 at 06:08
  • then place the PHP code in History.php before anything gets output. That is the usual way to do this. – Pekka May 24 '11 at 06:09
  • see this http://stackoverflow.com/questions/4483432/how-to-call-php-function – Harsh May 24 '11 at 06:10

4 Answers4

6

Top of History.php:

<?php
    include('/path/to/php/program/with/function.php');
    the_function();
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • If I include it in the top of the file, which has included the function(DB link) and if call that function will it execute befor the the page get execute. – Indu May 24 '11 at 06:11
  • yes... remember, it is your PHP file that sends anything back, so you have complete control. If your PHP page did nothing, the browser would get no response. – dyasta May 24 '11 at 06:15
  • Before it is executed? No - you can't call a function without being in the process executing it. Before it is downloaded? Yes — since it is at the top, there hasn't been any output, so there has been nothing generated for the browser to download. – Quentin May 24 '11 at 06:17
1

You can also try at top of:

Hisory.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 Quentin described.

Naveed
  • 41,517
  • 32
  • 98
  • 131
0

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).

dyasta
  • 2,056
  • 17
  • 23
0

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.

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().

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().

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.

Good luck!

inquam
  • 12,664
  • 15
  • 61
  • 101