|
-
Feb 12th, 2005, 01:44 AM
#1
Importing an other php file [Part2]
What if I have 2 included files.
First the index.php file inclused a file (that uses some php) then after that the index file it self uses some php. Then the index file includes an other file that uses PHP.
Is it then enough to open the database in the first included php file? Or should I open it up before the first included php file in the index file?
ØØ
-
Feb 12th, 2005, 05:47 AM
#2
Re: Importing an other php file [Part2]
 Originally Posted by NoteMe
What if I have 2 included files.
First the index.php file inclused a file (that uses some php) then after that the index file it self uses some php. Then the index file includes an other file that uses PHP.
Is it then enough to open the database in the first included php file? Or should I open it up before the first included php file in the index file?
ØØ
I think when it includes a file all it does is takes the code and puts it all together so it kind of builds one script from 3 (in yor case) as to where to put the database opening, I would say put it before the first include then its avalible through the rest of the script.
Hope that helps
-
Feb 12th, 2005, 06:19 AM
#3
<?="Moderator"?>
Re: Importing an other php file [Part2]
Hey have you tried using a class? I start out using a method like yours but then switched to using a class so that i wouldnt have to re-write the basics of connecting everytime i started a new project.
Its not the best code as im still perfecting it at the moment, and im sure there is a better way of doing what im doing
PHP Code:
<?php include_once("error.class.php"); if(!defined("DB.CLASS")) { define("DB.CLASS",true); class DBClass { var $server = "sql-server"; var $database = "database"; var $username = "username"; var $password = "password"; var $prefix = ""; var $DISPLAY_ERRORS = false; var $conn; var $ConnectionOpen = false; var $results; function connect() { $this->conn = @mysql_connect($this->server,$this->username,$this->password); if($this->conn != false) { if(@mysql_select_db($this->database,$this->conn)) { $this->ConnectionOpen = true; return true; } else { $e = new ERROR(mysql_error()); } } else { $e = new ERROR(mysql_error()); } $this->ConnectionOpen = false; if($this->DISPLAY_ERRORS) { //print("ERROR:" . mysql_error()); $e = new ERROR(mysql_error()); } return false; } function close() { $this->ConnectionOpen = false; if(!@mysql_close($this->conn)) { $e = new ERROR(mysql_error()); } } function ExecuteSQL($query) { if($this->ConnectionOpen) { $this->results = @mysql_query($query,$this->conn); if(!$this->results) { return true; } else { return false; } } } function ExecuteGetRow($query) { if($this->ConnectionOpen) { $results = @mysql_query($query,$this->conn); return @mysql_numrows($results); } } function InsertID() { if($this->ConnectionOpen) { return @mysql_insert_id(); } } function AffectedRows() { if($this->ConnectionOpen) { return @mysql_affected_rows($this->conn); } } function ReturnResults() { $array = Array(); while($row = @mysql_fetch_row($this->results)) { $temp = Array(); for($i = 0; $i < count($row); $i++) { $temp[@mysql_field_name($this->results,$i)] = $row[$i]; } $array[] = $temp; } return $array; } } } ?>
PHP Code:
<? $dbc = new DBClass(); $dbc->Connect(); if(!$dbc->ConnectionOpen) { $e = new ERROR(mysql_error()); //or your own error handling } else { $dbc->ExecuteSQL($query); print_r($dbc->ReturnResults()); } ?>
Just a thought for the future
-
Feb 12th, 2005, 11:19 AM
#4
Re: Importing an other php file [Part2]
 Originally Posted by Pino
I think when it includes a file all it does is takes the code and puts it all together so it kind of builds one script from 3 (in yor case) as to where to put the database opening, I would say put it before the first include then its avalible through the rest of the script.
Hope that helps
OK, so it first copy/asembly the files togheter then running the PHP? Or the other way around? SHould be the other way around if it should be able to import it at all. It is imported using PHP. Unless it first runs PHP to check for inclused. THen copy stuff togheter, and then start from the top again to run the PHP.
Hehehe...Think I even confused my self now..
ØØ
-
Feb 12th, 2005, 11:20 AM
#5
Re: Importing an other php file [Part2]
 Originally Posted by john tindell
Hey have you tried using a class? I start out using a method like yours but then switched to using a class so that i wouldnt have to re-write the basics of connecting everytime i started a new project.
Its not the best code as im still perfecting it at the moment, and im sure there is a better way of doing what im doing
PHP Code:
<?php
include_once("error.class.php");
if(!defined("DB.CLASS"))
{
define("DB.CLASS",true);
class DBClass
{
var $server = "sql-server";
var $database = "database";
var $username = "username";
var $password = "password";
var $prefix = "";
var $DISPLAY_ERRORS = false;
var $conn;
var $ConnectionOpen = false;
var $results;
function connect()
{
$this->conn = @mysql_connect($this->server,$this->username,$this->password);
if($this->conn != false)
{
if(@mysql_select_db($this->database,$this->conn))
{
$this->ConnectionOpen = true;
return true;
}
else
{
$e = new ERROR(mysql_error());
}
}
else
{
$e = new ERROR(mysql_error());
}
$this->ConnectionOpen = false;
if($this->DISPLAY_ERRORS)
{
//print("ERROR:" . mysql_error());
$e = new ERROR(mysql_error());
}
return false;
}
function close()
{
$this->ConnectionOpen = false;
if(!@mysql_close($this->conn))
{
$e = new ERROR(mysql_error());
}
}
function ExecuteSQL($query)
{
if($this->ConnectionOpen)
{
$this->results = @mysql_query($query,$this->conn);
if(!$this->results)
{
return true;
}
else
{
return false;
}
}
}
function ExecuteGetRow($query)
{
if($this->ConnectionOpen)
{
$results = @mysql_query($query,$this->conn);
return @mysql_numrows($results);
}
}
function InsertID()
{
if($this->ConnectionOpen)
{
return @mysql_insert_id();
}
}
function AffectedRows()
{
if($this->ConnectionOpen)
{
return @mysql_affected_rows($this->conn);
}
}
function ReturnResults()
{
$array = Array();
while($row = @mysql_fetch_row($this->results))
{
$temp = Array();
for($i = 0; $i < count($row); $i++)
{
$temp[@mysql_field_name($this->results,$i)] = $row[$i];
}
$array[] = $temp;
}
return $array;
}
}
}
?>
PHP Code:
<?
$dbc = new DBClass();
$dbc->Connect();
if(!$dbc->ConnectionOpen)
{
$e = new ERROR(mysql_error()); //or your own error handling
}
else
{
$dbc->ExecuteSQL($query);
print_r($dbc->ReturnResults());
}
?>
Just a thought for the future
We never looked much into classes in our web class. Didn't have much time for it, and since PHP 5 was around the corner and it was supposed to have much better OO the theacher didn't bother too much about it.
But your code looks good. Is it PHP 4.x?
-
Feb 12th, 2005, 09:36 PM
#6
<?="Moderator"?>
Re: Importing an other php file [Part2]
its PHP 4.3 i think, but dont quote me on that. It works for me but it might not work for you just a thought anyway
-
Feb 13th, 2005, 04:32 AM
#7
Re: Importing an other php file [Part2]
It won't work if you don't post the Error class too, as the DB object uses it.
-
Feb 13th, 2005, 04:44 AM
#8
Re: Importing an other php file [Part2]
Like Pino said, if you include a file, it takes the code from the file and insterts it at the point. Therefore it inherits, as it were, the current scope of the code.
So if you include a file inside a class function then you will inherit the scope of that function, inlcuding its local variables and the scope of the class. If you include a file inside a function, it will inherit the scope of that function and all its local variables and finally if you include a file globally, you inherit all the global variables.
-
Feb 13th, 2005, 05:41 AM
#9
<?="Moderator"?>
Re: Importing an other php file [Part2]
 Originally Posted by visualAd
It won't work if you don't post the Error class too, as the DB object uses it.
woops forgot that 
its only to notify me if something goes wrong. still working on it, like everything else
PHP Code:
<?php if(!defined("ERROR.CLASS")) { define("ERROR.CLASS",true); class ERROR extends CONFIG { var $Error_EmailAddress = "[email protected]"; function ERROR($message) { $msg = $message; $msg .= "\n\r" . $_SERVER['REMOTE_ADDR']; $msg .= "\n\r" . date("D M j G:i:s T Y",time()); $headers = "From: ". $Error_EmailAddress."<" . $Error_EmailAddress . ">\r \n"; @mail($this->Error_EmailAddress,"ERROR",$msg,$headers); print "<pre>$msg</pre>"; } } } ?>
-
Feb 13th, 2005, 06:47 AM
#10
Re: Importing an other php file [Part2]
What about the CONFIG class, which the ERROR class is derived from?
Personally I would not use the DB class you have created. The main reason is, is that is is changing the API needed to access the database. One of the beauties of PHP is that all its database functions regardless of the vendor are near identical and return identical results. This means that you can change the database you are using with minimum hassle.
The idea of wrapping this functionality into an abstraction layer in the form of a class is great, but there are few things that, are not only easy to implement, but will make it a lot more flexible.
Take a look at how similar the functions are for the MySql sever and MS Sql server API in PHP. This is crying out polymorphism and in theory you cna make several database calsses which all implement the same interface for different database vendors. e.g:
PHP Code:
class MySqlDB
{
}
class MSSqlDB
{
}
class PostgreSQLDB
{
}
All these classes need to do is wrap the functions for each database and return exactly the same output. You can then transpraently switch the the vendor of the database without having to change a line of code and have code like this:
PHP Code:
switch ($_GET['db'])
{
case 'MySql':
$db = new MySqlDB;
break;
case 'PostgreSQL':
$db = new PostgreSQLDB;
break;
case 'MSSQL':
$db = new MSSqlDB;
break;
}
if (! $result = $db->query('SELECCT user_name FROM users')) {
die($db->error());
} else {
while ($row = $db->fetch_row($result)) {
echo($row[0]);
}
}
Its nice to see I'm not the only one who uses classes in PHP though and the same logic can also be applied to your class to allow you to switch the database type easily, I just think it lacks some of the functionality and will mean you will have to go back to the native function calls for this, which defeats the whole purpose.
-
Feb 13th, 2005, 06:53 AM
#11
<?="Moderator"?>
Re: Importing an other php file [Part2]
yer your right, I think ill either end up completely changing the whole thing a million times, or just scrapping it and becomming a hobo
What do you think about PHP5 having Public and Private for the classes?
-
Feb 13th, 2005, 07:10 AM
#12
Re: Importing an other php file [Part2]
 Originally Posted by john tindell
What do you think about PHP5 having Public and Private for the classes?
PHP 5 OOP is 100% better then PHP4. Its now fully OO whereas PHP 4 misses the encapsulation side of things. It also allows you to create interfaces and abstract classes which means your code is less prone to errors.
The main thing is that objects are now by defualt treated as references. So when you assign an object ot a variable, it actually assigns that object and not a copy of it.
Your class would be easy to change. If you are worried about it breaking other code which depends on it then just keep the orinigal interface and add to it.
-
Feb 13th, 2005, 07:19 AM
#13
<?="Moderator"?>
Re: Importing an other php file [Part2]
 Originally Posted by visualAd
Your class would be easy to change. If you are worried about it breaking other code which depends on it then just keep the orinigal interface and add to it.
That was one of the main reasons i used a class so i could change it completely when i discover a better way.
 Originally Posted by visualAd
PHP 5 OOP is 100% better then PHP4. Its now fully OO whereas PHP 4 misses the encapsulation side of things. It also allows you to create interfaces and abstract classes which means your code is less prone to errors.
The main thing is that objects are now by defualt treated as references. So when you assign an object ot a variable, it actually assigns that object and not a copy of it.
I havent really looked into PHP5 that much, i was speak to one of the tutors at my college and he was saying bad stuff about PHP5 being too OO so i didnt really look into it anymore.
So does PHP5 look like Java or C# now?
-
Feb 13th, 2005, 08:59 AM
#14
Re: Importing an other php file [Part2]
PHP5 can still be completely un-OOP if that's what you desire, so your tutor's rant was unjustified. But PHP5 supports proper OOP, unlike PHP4.
So yes, it's much more like Java, but not stricly typed.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|