[英]How to share php variables in 2 different php files
here is the code for first.php 这是first.php的代码
<?php
mysql_connect("localhost","root",""); // host, username, password...
mysql_select_db("testdb");
$v1=$_REQUEST['usn'];
$q=mysql_query("select * from users where usn='$v1'");
while($row=mysql_fetch_assoc($q))
$json_output[]=$row;
$json_output["re"]="success";
print(json_encode($json_output));
mysql_close();
?>
code for Second.php Second.php的代码
<?php
mysql_connect("localhost","root",""); // host, username, password...
mysql_select_db("testdb"); // db name...
$q=mysql_query("select * from users where usn='$v1'");
while($row=mysql_fetch_assoc($q))
$json_output[]=$row;
print(json_encode($json_output));
mysql_close();
?>
Now how to use the value of variable $v1 ,declared in first.php, in second.php.The value of $v1 is obtained through user's input via EditText widget in android. 现在如何使用在first.php,second.php中声明的变量$ v1的值。$ v1的值是通过android中EditText小部件通过用户输入获得的。
I tried even with session variables but still not getting O/P: 我什至尝试使用会话变量,但仍然无法获得O / P:
first.php first.php
<?php
session_start();
mysql_connect("localhost","root",""); // host, username, password...
mysql_select_db("testdb");
$_SESSION["v1"]=$_REQUEST['usn'];
$q=mysql_query("select * from users where usn='".$_SESSION["v1"]."'");
while($row=mysql_fetch_assoc($q))
$json_output[]=$row;
$json_output["re"]="success";
print(json_encode($json_output));
mysql_close();
?>
second.php second.php
<?php
session_start();
$v3 = $_SESSION["v1"];
mysql_connect("localhost","root",""); // host, username, password...
mysql_select_db("testdb"); // db name...
$q=mysql_query("select * from users where usn='$v3'");
while($row=mysql_fetch_assoc($q))
$json_output[]=$row;
print(json_encode($json_output));
mysql_close();
?>
Well you can use sessions for more see here PHP Sessions 那么你可以使用更多的看到这里的会话PHP会议
here is the fixed code for first.php 这是first.php的固定代码
<?php
//Start session
session_start();
mysql_connect("localhost","root",""); // host, username, password...
mysql_select_db("testdb");
$v1=$_REQUEST['usn'];
//Set Session data
$_SESSION['v1']=$v1;
$q=mysql_query("select * from users where usn='$v1'");
while($row=mysql_fetch_assoc($q))
$json_output[]=$row;
$json_output["re"]="success";
print(json_encode($json_output));
mysql_close();
?>
code for Second.php Second.php的代码
<?php
session_start();
mysql_connect("localhost","root",""); // host, username, password...
mysql_select_db("testdb"); // db name...
//Read from session
$v1 = $_SESSION['v1'];
$q=mysql_query("select * from users where usn='$v1'");
while($row=mysql_fetch_assoc($q))
$json_output[]=$row;
print(json_encode($json_output));
mysql_close();
?>
in first.php
you hav to save $v1
in a session variable .. like this 在first.php
您必须将$v1
保存在会话变量中。
<?php
session_start(); // should be on top of your script
$v1 = $_REQUEST['usn'];
$_SESSION['v1'] = $v1;
?>
in Second.php
you can use $_SESSION['usn']
在Second.php
您可以使用$_SESSION['usn']
<?php
session_start();
$v1 = $_SESSION['v1'];
$q=mysql_query("select * from users where usn='$v1'");
?>
NOTE : use of mysql_*
is deprecated .. use mysqli
注意 :不建议使用mysql_*
..使用mysqli
make a config.php file and place all common variables and values there 制作一个config.php文件并将所有公共变量和值放在此处
then require_once('config.php'); 然后require_once('config.php'); on the other php file 在另一个PHP文件
eg 例如
config.php
<?
$v =1;
?>
second.php
<?
require_once('config.php');
echo $v;
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.