new,delete and function.

hi friends.
if i have in the same page to call different variables and different
function, how can i do??..
so, i have a function in a file called function.php,
and i'd like to call the function in it more of 1 time
in the same php file..

<p style="color: #1F86DE; font-size: 15px; padding-bottom: 0px;"><b>
<? include ("./source/function.php"); $id = '1'; $cosa = 'Title'; echo get($id, $cosa); ?>
</b></p>
<P>
<? $id = '1';$cosa = 'Description'; echo get($id, $cosa); ?>
<BR>
</p>

this is my code, but return an error..
this error :

Fatal error: Cannot redeclare get() (previously declared in \www\\portal\source\function.php:3) in c:\programmi\easyphp1-\www\\portal\source\function.php on line 3

i must use the new and delete operator..
anyone can help me??..
thanks
[962 byte] By [Rooting] at [2007-11-20 11:02:46]
# 1 Re: new,delete and function.
Are you sure your include statement isn't in a loop? Called more than once?

Your problem isn't calling the function but you are declaring it more than once. The following example would duplicate your error.

function duplicate(){
echo 'Something';
}
function duplicate(){
echo 'Something';
}
PeejAvery at 2007-11-10 3:56:10 >
# 2 Re: new,delete and function.
mmm..no..
the function get is declared 1 time in the function.h..

if in index.php I write this

<p style="color: #1F86DE; font-size: 15px; padding-bottom: 0px;"><b>
<? include ("./source/function.php"); $id = '1'; $cosa = 'Title'; echo get($id, $cosa); ?>
</b></p>
<P>
<BR>
</p>

there are't error..
so the error is in that espression

<? $id = '1';$cosa = 'Description'; echo get($id, $cosa); ?>

whot's is wrong??..
Rooting at 2007-11-10 3:57:06 >
# 3 Re: new,delete and function.
Well, can you post the source of your function.php file? Also, I would suggest using <?php ... ?> instead of <? ?>.
PeejAvery at 2007-11-10 3:58:15 >
# 4 Re: new,delete and function.
sure..
this is the function of function.php

<?php

function get($id, $cosa) {
include ("var_db.php");

$connessione = mysql_connect($host, $username, $password) or exit("!ERROR");
mysql_select_db("dev-archive") or die ("!ERROR");
$query = mysql_query("SELECT * FROM home WHERE id = '".$id."'");
$result = mysql_fetch_object($query);

return $result->$cosa;
}


?>
Rooting at 2007-11-10 3:59:08 >
# 5 Re: new,delete and function.
Is this all the code in function.php? Also, you have an includes there. How many different PHP files are associated with this project?

Somewhere, in the middle of all that code, it is trying to read the function get() twice. It could be written twice in other include files. Or it could be looping somehow to read that section of code twice.
PeejAvery at 2007-11-10 4:00:16 >
# 6 Re: new,delete and function.
yes this is the files function.php..1 function..
in var_db there are 3 variables

<?php
$host = "localhost";
$username = "root";
$password = "";

?>

i don't understand whot you say...
how i can to call function 2 or more time??..
whithout errors??XD

thanks!!
Rooting at 2007-11-10 4:01:15 >
# 7 Re: new,delete and function.
You can call a function more than once without errors. As I stated in my first post...your problem is not calling a function, it is declaring it.

Somewhere, your code is attempting to read the line function get(...) more than once. Since we don't have your whole source code, we here at CG cannot tell you exactly where your problem is. So you need to look through your source and find out why it is reading that line twice.
PeejAvery at 2007-11-10 4:02:14 >
# 8 Re: new,delete and function.
if i do it :

<p style="color: #1F86DE; font-size: 15px; padding-bottom: 0px;"><b>
<? include ("./source/function.php"); $id = '1'; echo get($id); ?>
</b></p>
<P>
<? echo get($id); ?>
<BR>
</p>

is right, so isn't the function..
but tghe $id variables..
if i do it:

<p style="color: #1F86DE; font-size: 15px; padding-bottom: 0px;"><b>
<?php include ("./source/function.php"); $id = '1'; echo get($id); ?>
</b></p>
<P>
<?php $id = '2'; echo get($id); ?>
<BR>
</p>

there are an error...

so..there in c++ for allocate a memory for another variables
i use new..
in php??
Rooting at 2007-11-10 4:03:16 >
# 9 Re: new,delete and function.
I warned you about the include inside the function in one of my previous posts.

Change the following...
<?php
function get($id, $cosa) {
if(!isset($included_var_db)){include ("var_db.php");}
$included_var_db = true;

$connessione = mysql_connect($host, $username, $password) or exit("!ERROR");
mysql_select_db("dev-archive") or die ("!ERROR");
$query = mysql_query("SELECT * FROM home WHERE id = '".$id."'");
$result = mysql_fetch_object($query);

return $result->$cosa;
}
?>
PeejAvery at 2007-11-10 4:04:14 >
# 10 Re: new,delete and function.
now he show an error like this

!ERROR

so he can't connect to db..

heare is the source..

index.php

....
<p style="color: #1F86DE; font-size: 15px; padding-bottom: 0px;"><b>
<? include ("./source/function.php"); $id = '1'; $cosa = 'Titolo'; echo get($id, $cosa); ?>
</b></p>
<P>
<? $id = '1'; $cosa = 'Titolo'; echo get($id, $cosa);; ?>
<BR>
</p>
...

./source/function.php

<?php
function get($id, $cosa) {

if(!isset($included_var_db)){include ("var_db.php");}
$included_var_db = true;

$connessione = mysql_connect($host, $username, $password) or exit("!ERROR");
mysql_select_db("dev-archive") or die ("!ERROR");
$query = mysql_query("SELECT * FROM home WHERE id = '".$id."'");
$result = mysql_fetch_object($query);

return $result->$cosa;
}
?>

./source/var_db.php

<?php

$host = "localhost";
$username = "root";
$password = "";
$db = "dev-archive";

?>

whot's is wrong?? :|
Rooting at 2007-11-10 4:05:21 >
# 11 Re: new,delete and function.
It might not have liked the variable declared in the function first. Try...
<?php
$included_var_db = false;
function get($id, $cosa) {

if(!$included_var_db){include ("var_db.php");}
$included_var_db = true;

$connessione = mysql_connect($host, $username, $password) or exit("!ERROR");
mysql_select_db("dev-archive") or die ("!ERROR");
$query = mysql_query("SELECT * FROM home WHERE id = '".$id."'");
$result = mysql_fetch_object($query);

return $result->$cosa;
}
?>
PeejAvery at 2007-11-10 4:06:17 >
# 12 Re: new,delete and function.
thanks lots...

i resolved it!!!!!!!!!....
you're genius!!!!..
Rooting at 2007-11-10 4:07:25 >
# 13 Re: new,delete and function.
Glad to hear it! Good luck. :thumb:
PeejAvery at 2007-11-10 4:08:21 >