Hi all,
wanted to ask you please to tell me if youcan the exact detailed deference between : include(), require(), require_once() and lot of thanks in advance ...
[if example of each explaining the deference is better]
thanks in advance....
Hi all,
wanted to ask you please to tell me if youcan the exact detailed deference between : include(), require(), require_once() and lot of thanks in advance ...
[if example of each explaining the deference is better]
thanks in advance....
require() and include() are identical with the exception on how they act if they fail.
if require() fails (e.g.: the file does not exist) then a Fatal Error occurs.
if include() fails then a Warning (non-fatal) occurs.
require_once() and include_once() only process the file once. So you can call require_once() three times in a row with the same filename, but it is only loaded and parsed once.
The alternative is to use include() and require() and in those files have like:
<?php
if (!isset($include_<script name minus extension>_once)) {
$include_<script name minus extension>_once = 1;
... code goes here ...
}
?>
where <script name minus extension> is just a way to make it unique for each file (e.g.: "view", "links", etc.)
require_once() and include_once() are better because even though the "if {}" logic prevents double-parsing/execution of the code, the file is still re-loaded and re-parsed each time you require() and include() and I've heard that require()'s and include()'s are among the most expensive function calls to make in PHP for this very reason.
Hope that helps.
just use require once!
thats all you need
Remember, if a fata error is thrown the script will stop executing and the page won't load (any further).
I always use the '_once' versions (never know when you might have a repetitive call) and 'include' when loading optional code (like a shoutbox maybe) and require when the page would be useless without the code I am calling.
thanks ...
the point is when i want to connect to database, i usually keeps the database connection details in a separate file as if : db.php
and then i call it by include, because i may use it multiple times in one page ..
right .. ?
No - once you do the include, it is just like the code was written on the page. It doesn't go anywhere after executing, so you could run it as often as you needed.
A DB connection is a great example of when I would use 'require_once' - the script will will be worthless without it.
so,
if you are having lot of database connections in the same page, you have to require_once alot ... right ?
and that as i think increases the size of the file because require_once, makes the code inside the page to be excuted once .. right ..? then the load of page will take a bit more ...
but when you use the include, that means code will be loaded once for all the connections you'll made in this page ..
is that right .. ?
if your case I would write some sort of wrapper function in a separate file like-so:
function DBconnect() {
static $db_link = null;
if (!is_resource($db_link)) {
$db_link = @mysql_connect('...');
if (!$db_link) {
trigger_error('Could not connect to database', E_USER_ERROR);
}
}
return $db_link;
}
require_once() the script where you put this function, and use it like so:
require_once('db.php');
// fetches current database connect or creates a new one
// if it does not exist
$db_link = DBconnect();
... work with the connection here
The above code is very rough, but you should get the idea.
You could go one further and create a class to store all your connection information, such as your username, password, host, port, etc. as well as provide database-agnostic functions so in the future you can switch out MySQL with PostgreSQL (or PostgreSQL to MySQL or whatever) and you only have to replace out the method code instead of hunting down all the mysql or psgl function calls in your code.
Or even just make a function that returns the information as an array:
function DBgetConnectionInfo() {
return array('host' => 'localhost',
'port' => 3306,
'username' => 'myuser',
'password' => 'mypass');
}
Instead of rolling your own, you can look at solutions such as Pear:B:
Not quite. If you weren't going to call it as an include, would you type it over and over, or would you just execute it when you needed it?
<? require_once 'DB.php';
//this file contains your db connection functions like dbconnect, dbinsert, etc..
//do an insert
dbConnect();
$insertquery = "INSERT INTO table (field) VALUES (data)";
dbInsert($query);
dbClose();
?>
some html
<?
//do a select
dbConnect();
$selectquery="SELECT * FROM table";
dbFetch($selectquery);
dbClose();
?>
Once the functions are included in the page you can call them as often as you want.
thanks alot,
understood ...
now some thing else came up tp mind ..
i was doing some long conversation there in another thread : class, asking about classes, because i dont use classes usually ...
and after the guys show me how and some other tutorials, i think i made a mistake in my first class ...
if you guys can help me thanks alot (any way thanks ):
i have tried to create a class to make a connection to database is OBJECT and to pass the host name and username and pass ... and handle some processing, as well as to select a certain database, and to do a query ... etc ...
i think i have did some thing wrong, and may be correcting me in here, and telling me what was my mistake and where will complete making me understanding the class i thank you really, and dont know what to do without you guys ..
here is what i have wrote in the class :
<?php
class db {
var $thehost; #Host Name
var $theusername; #the username of the database connection
var $pass; #the password of the connection
var $dbcon; #to connect to database FALSE or TRUE
var $dbs; #to select a database
var $dbno; #if failed to connect to database the value is 1
var $dbsno; #if failed to select database the value is 1
var $statement; #the MySQL statement
var $query; #doing the MySQL query
var $qno; #if query is not okkay it gets the value 1
# the function to connect to database
function connectdb($this->thehost,$this->theusername,$this->pass)
{
$this->dbcon=@mysql_connect($this->thehost,$this->theusername,$this->pass);
if (!$this->dbcon){
echo("Unable to connect to Database .. Call webmaster");
$this->dbno=1;
}else{
$this->dbno=0;
}
}
# the function to select to database
function selectdb()
{
$this->dbs=mysql_select_db("ab4net");
if (!$this->dbs){
echo("Error Sellecting DB .. Call webmaster");
$this->dbsno=1;
}else{
$this->dbsno=0;
}
}
# the function to process the query
function getresult($this->statement,$this->dbcon)
{
$this->query=mysql_query($this->statement,$this->dbcon);
if(!$this->query){
echo("Error in SQL Statement .. Call webmaster<br>");
echo mysql_error()n;
$this->qno=1;
}else{
$this->qno=0;
}
}
}
?>
lot of thanks ...
change:
function connectdb($this->thehost,$this->theusername,$this->pass) {
to
function connectdb($arghost,$argusername,$argpass) {
$this->thehost = $arghost;
$this->theusername = $argusername;
$this->pass = $argpass;
and change:
getresult($this->statement,$this->dbcon) {
to
getresult($argstatement) {
$this->statement = $argstatement;
// no need to pass in the dbcon as it is already assigned in the connect() method
You can't have "$this->" as an argument to the function.
"$this" is a special variable that refers to the current instance of the class, and is only available inside the class methods.
thanks alot,
i'll try it ....
and come back to you ...
thanks master
many greatings ...