View Full Version : The assignments here influenced and other php files using the variables ?

06-04-2007, 11:32 AM
The assignments here [eg. $first=mysql_result($result,$i,"first");] are influenced and other php files using the variables[eg. $first] ? eg.
If not how to use the variables[eg. $first] in another file or files in the dmwr site ?

@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM contacts";



echo "<b><center>Database Output</center></b><br><br>";

while ($i < $num) {


echo "<b>$first $last</b><br>Phone: $phone<br>Mobile: $mobile<br>Fax: $fax<br>E-mail: $email<br>Web: $web<br><hr><br>";



06-05-2007, 12:00 AM
I'm not sure if I understand your question correctly, but you are asking if you can use $first and other variables outside of this script correct?
Is so, the answer is yes, you can by using an include() or require() call.
Be careful however, as all the declared variables are usable and overwritable in the using scripts - this includes your database information. It would be wise for you to unset these values prior to using an include.
A better method would be to use functions to return values that you are looking for, or in this case an array of values. The script would still require inclusion, however all the values it provides are generated on call as opposed to arbitrarily being forced in.

06-06-2007, 12:02 PM
How unset te values ? Can you give an example of using functions / include() ?

06-06-2007, 06:20 PM


$username = "Username";
$password = "Password";

// Do sql stuffs here
$result = mysql_result($mysqlStuffs);

unset($username, $password);


in another script

include 'path/to/file.php';

The point that the inclusion is called the values are now available. Unsetting the username and password results in neither being accessible outside of the called script.
If sql is used commonly, which it often is, you may want to consider scripting an object for it, preferably a php5 object if you have php 5 available, however, the php 4 engine would do. This way the sql connection can be established in multiple scripts without needing to reconstruct the entire call every time.

06-17-2007, 05:29 PM
Are any other ways besides this to include vars or functions without the rest of script ?
session() ?

06-17-2007, 08:39 PM
I mean we have two scripts:
script 1:
<? ...?>
script 2:
<? ...?>
I want vars/functions (script 1), some not all , get used in script 2. As I say I do NOT need some code of script 1... exist any other way ?