How can i pass variables between 2 php files?

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
leronj23Asked:
Who is Participating?
 
RoonaanCommented:
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
 
oosatanooCommented:


Can you include dbtv.php into get_users.php?

eg:
include /path/to/dbtv.php
0
 
soapergemCommented:
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
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
dr_dedoCommented:
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
 
leronj23Author Commented:
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
 
dr_dedoCommented:
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
 
leronj23Author Commented:
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
 
leronj23Author Commented:
$datadase and $tables are how people choose which table they want to pull information from in my sql server
0
 
dr_dedoCommented:
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
 
leronj23Author Commented:
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
 
leronj23Author Commented:
the above code didnt work. sorry to be a hassle
0
 
dr_dedoCommented:
can flash read variables sent via get ? i have not knowledge with actionscript!
what error did it raise ??
0
 
RoonaanCommented:
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
 
hiteshgupta1Commented:
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
 
RoonaanCommented:
No you can't. You would have notice that when you actually had read the whole thread.

-t-
0
 
leronj23Author Commented:
nothing is working for me. i guess im going to have to use 50 php files.
0
 
RoonaanCommented:
If nothing is working, then your last option would be to send the database and table variable in each sendAndLoad request.

-r-
0
 
leronj23Author Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.