?
Solved

How can i pass variables between 2 php files?

Posted on 2006-05-15
18
Medium Priority
?
294 Views
Last Modified: 2009-12-16
Here is my problem. I have 2 php files: "dbtb.php"  and "get_users.php".

I need to get the variables:"$database" and "$tables" from "dbtb.php" to load into "get_users.php".

$database and $tables are the variables that connect to my sql server.

please help.

Very Very Very Urgent. 500 points
0
Comment
Question by:leronj23
  • 7
  • 4
  • 4
  • +3
18 Comments
 
LVL 1

Expert Comment

by:oosatanoo
ID: 16686909


Can you include dbtv.php into get_users.php?

eg:
include /path/to/dbtv.php
0
 
LVL 6

Expert Comment

by:soapergem
ID: 16686946
You have choices:
1.) Include the file directly, as pointed out by oosatanoo,
2.) Send it as form data; e.g. call get_users.php?database=something&tables=somethingelse
3.) Use session variables, as described here:

Use the function session_start(); at the beginning of dbtb.php, and then do this:
$_SESSION['database'] = $database;
$_SESSION['tables'] = $tables;

Then, when you call get_users.php, you can call these $_SESSION variables and the values will still be stored.
0
 
LVL 16

Expert Comment

by:dr_dedo
ID: 16686986
to do that, u don't pass such variables between pages, as previous posts, you have to do an include

e.g
conntion.inc.php <<< this will contain
<?
$con = mysql_connect ('localhost','name','password');
$con = mysql_select_db ('db_name');
?>

in any page in the site that u want to connect to a db, you just inlude that file

index.php

<?
include ('conntion.inc.php');

//now u just run your query
$result = mysql_query ('select * from ............');
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:leronj23
ID: 16687184
thanks for all of your suggestions but im new to php and know just basic stuff and im confused.

here is my: "dbtb.php"

<?
$database = $_POST['db'];
$tables = $_POST['tb'];
?>

here is my: "get_user.php"

<?php
$server = "mysql";
$user = "leronj23";
$pass = "ltrain23";

$databases = $database;  //   i need the variables tranfered here
$tableName = $tables;     //    i need the variable transfered here


$dbc = @mysql_connect($server,$user,$pass)OR die ('Could not connect to MySQL: ' . mysql_error() );

$databases = @mysql_select_db($databases,$dbc)OR die ('Could not connect to MySQL: ' . mysql_error() );

$query = @mysql_query("SELECT * FROM $tableName");


these variables are coming from flash 8.
this should give you guys a better understand what im trying to do. thanks in advance.
0
 
LVL 16

Expert Comment

by:dr_dedo
ID: 16687235
how did $database and tables turn out to be post variables ?? were they filled by a user in a form ???
if so, the you have to get server name, user name and pass each in a separate variable

what does $database and $tables represent ??
0
 

Author Comment

by:leronj23
ID: 16687255
I have global variable in flash that i "POST" into "dbtb.php:
loadVariables("dbtb.php","POST");

"dbtb.php" looks like this:
<?
$database = $_POST['db'];
$tables = $_POST['tb'];
?>

now i need those variables to go over to "get_user.php"
0
 

Author Comment

by:leronj23
ID: 16687266
$datadase and $tables are how people choose which table they want to pull information from in my sql server
0
 
LVL 16

Expert Comment

by:dr_dedo
ID: 16687299
mmmm, $database  and tables are just strings then, nice
why dont you post these variables directly to get_user.php? why to dbtb.php first ??
anyway, i'm sure you got a good reason for that

so now we want
1- transfer these data from dbtb.php
2- make use of them in get_user.php

1- to transfer them, either via sessions of passed through the url, i prefere being passed in ur, so users can bookmark this page or change the table name on the fly

simply in dbtb.php use this line

header ("Locaion:get_user.php?database={ $_POST['db']}&table={$_POST['tb']}"); //this line requrie that there is nothing sent to the browser,  no text or even a blank space

2- in get_user.php
<?php
$server = "mysql";
$user = "leronj23";
$pass = "ltrain23";
$databases = $_GET['database'];  //   i need the variables tranfered here
$tableName = $_GET['table'];     //    i need the variable transfered here


$dbc = @mysql_connect($server,$user,$pass)OR die ('Could not connect to MySQL: ' . mysql_error() );

$databases = @mysql_select_db($databases,$dbc) OR die ('Could not connect to MySQL: ' . mysql_error() );

$query = @mysql_query("SELECT * FROM $tableName");
..........
.........

0
 

Author Comment

by:leronj23
ID: 16687358
basicly whats going on is this.

the first movie post the variables that is picked from the customer in "dbtb.php"
loadVariables("dbtb.php","POST");

then once the customer picks the city and category they want. it goes to a second movie which loads the information from a database in sql "get_user.php".

"get_user.php" is called from:
sender.sendAndLoad("get_users.php",receiver,"POST");

this put information into a datagrid in flash.

so basicly when i call for "get_user.php" thats when i need the variables from "dbtb.php"
0
 

Author Comment

by:leronj23
ID: 16687420
the above code didnt work. sorry to be a hassle
0
 
LVL 16

Expert Comment

by:dr_dedo
ID: 16687438
can flash read variables sent via get ? i have not knowledge with actionscript!
what error did it raise ??
0
 
LVL 49

Expert Comment

by:Roonaan
ID: 16687982
leronj23,

php scripts aren't persistent. When you first call dbtd.php and then the other script, the details of your database are lost after you close the connection to dbtd.php.

Personally I would define several database profiles in php:
<?php
  $profiles['profile1'] = array('host' => 'something', 'user' => 'username', 'password' => 'password', 'database' => 'mydatabase');
?>

In actionscript you then would select one of these profiles in each and every loadVariables/sendAndLoad you do:

sender.databaseProfile = 'profile1';

Another approach might be to use sessions.

Have dtdb contain something like:
<?php
  session_start();
  $_SESSION['database'] = $_POST['database'];
  $_SESSION['table'] = $_POST['table'];
  echo '&sessionid='.session_id();
?>

In actionscript you then need to add sessionid as additional variable:

sender.sessionid = _root.sessionid; //assuming loadVariables target was _root.

And in get_users.php you would use:

<?php
  if(isset($_POST['sessionid'])) session_id($_POST['sessionid']);
  session_start();
  $database = $_SESSION['database'];
  $table = $_SESSION['table'];
?>

I hope this helps. Please post back with any questions.

-r-
0
 
LVL 8

Expert Comment

by:hiteshgupta1
ID: 16688124
IF the variabnles are strin then u can also store them in a hidden input and u can directly access the values in the second page
or else u can treat them as session variables too
0
 
LVL 49

Expert Comment

by:Roonaan
ID: 16688146
No you can't. You would have notice that when you actually had read the whole thread.

-t-
0
 

Author Comment

by:leronj23
ID: 16690197
nothing is working for me. i guess im going to have to use 50 php files.
0
 
LVL 49

Expert Comment

by:Roonaan
ID: 16690265
If nothing is working, then your last option would be to send the database and table variable in each sendAndLoad request.

-r-
0
 

Author Comment

by:leronj23
ID: 16690511
the code is below:
whats going on is that im using flash 8. this code pulls information from "get_user.php" file that connects to my sql server and then brings it back to display in a datagrid on my site. how can i include the variables "database" and "tables" into this code to be sent when this information is sent to "get_user.php" file to call a specific database and table in sql?


var myGridArray:Array = [];
myGridArray.push({status:"loading data..."});
myGrid.dataProvider = myGridArray;
var myDataHolder:Array = [];
var sender:LoadVars = new LoadVars();
var receiver:LoadVars = new LoadVars();
receiver.onLoad = function(ok){
     if(ok){
         myGrid.removeAllColumns();
         myGrid.removeAll();
         for(var i =1;i<=receiver.total;i++){
             receiver["dataPacket"+i] = receiver["user_data"+(i)].split("|");
             var _id:String = receiver["dataPacket"+i][0];
                   var _dates:String = receiver["dataPacket"+i][1];
             var _companyname:String = receiver["dataPacket"+i][2];
             var _address:String = receiver["dataPacket"+i][3];
             var _city:String = receiver["dataPacket"+i][4];
                   var _states:String = receiver["dataPacket"+i][5];
                   var _zipcode:String = receiver["dataPacket"+i][6];
                   var _contact:String = receiver["dataPacket"+i][7];
                   var _phone:String = receiver["dataPacket"+i][8];
                   var _fax:String = receiver["dataPacket"+i][9];
                   var _email:String = receiver["dataPacket"+i][10];
                   var _positions:String = receiver["dataPacket"+i][11];
                    var _salary:String = receiver["dataPacket"+i][12];
                   var _fullpart:String = receiver["dataPacket"+i][13];
                   var _messages:String = receiver["dataPacket"+i][14];
             myGridArray.push({ Topic:_positions, Dates:_dates });
             var dataObj:Object = {};
             dataObj.dates = _dates;
                   dataObj.companyname = _companyname;
             dataObj.address = _address;
             dataObj.city = _city;
                   dataObj.states = _states;
                   dataObj.zipcode = _zipcode;
                   dataObj.contact = _contact;
                   dataObj.phone = _phone;
                   dataObj.fax = _fax;
                   dataObj.email = _email;
                   dataObj.positions = _positions;
                   dataObj.salary = _salary;
                   dataObj.fullpart = _fullpart;
                   dataObj.messages = _messages;
             myDataHolder.push(dataObj);
             delete(receiver["user_data"+i]);
         }
         myGrid.dataProvider = myGridArray;
         setupGrid();
     }else{
         myGrid.removeAllColumns();
         myGrid.removeAll();
         myGridArray.push({status:"No data was found!"});
         myGrid.dataProvider = gridArray;
     }
}

sender.sendAndLoad("get_users.php",receiver,"POST");

0
 
LVL 49

Accepted Solution

by:
Roonaan earned 2000 total points
ID: 16690623
Try:

sender.database = "somedatabase";
sender.table = "sometable";
sender.sendAndLoad("get_users.php", receiver, "POST");

Then in php check the $_POST['database']  and $_POST['table'] variables.

-r-

0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses
Course of the Month14 days, 19 hours left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question