Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    Dec 2005
    Posts
    702
    Thanks
    0
    Thanked 0 Times in 0 Posts

    The assignments here influenced and other php files using the variables ?

    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++;
    }

    ?>

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    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.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #3
    Regular Coder
    Join Date
    Dec 2005
    Posts
    702
    Thanks
    0
    Thanked 0 Times in 0 Posts
    How unset te values ? Can you give an example of using functions / include() ?

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    sure
    PHP Code:
    <?php

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

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

    unset(
    $username$password);

    ?>
    in another script
    PHP Code:
    ...
    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.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #5
    Regular Coder
    Join Date
    Dec 2005
    Posts
    702
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Are any other ways besides this to include vars or functions without the rest of script ?
    session() ?

  • #6
    Regular Coder
    Join Date
    Dec 2005
    Posts
    702
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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 ?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •