...

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



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

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM contacts";
$result=mysql_query($query);

$num=mysql_numrows($result);

mysql_close();

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

$i=0;
while ($i < $num) {

$first=mysql_result($result,$i,"first");
$last=mysql_result($result,$i,"last");
$phone=mysql_result($result,$i,"phone");
$mobile=mysql_result($result,$i,"mobile");
$fax=mysql_result($result,$i,"fax");
$email=mysql_result($result,$i,"email");
$web=mysql_result($result,$i,"web");

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

$i++;
}

?>

Fou-Lu
06-04-2007, 11:00 PM
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.

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

Fou-Lu
06-06-2007, 05:20 PM
sure


<?php

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

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

lse123
06-17-2007, 07: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 ?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum