2014-04-09 43 views
0

这里是first.php如何共享PHP变量在2个不同的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(); 

?> 

代码的代码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(); 

?> 

现在如何使用变量$ V1的值,在first.php中声明在second.php中。$ v1的值是通过用户通过android中的EditText小部件输入获得的。

我与会话变量,甚至试过,但还是没有得到O/P:

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

<?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(); 

    ?> 
+0

杜佩:http://stackoverflow.com/questions/18238409/how-to-use-a-variable-in-2-different-php-files? rq = 1 –

+2

首先通过使用mysqli而不是mysql被depracted开始,而不是在每个文件中打开你的mysql连接 – SuperDJ

回答

2

那么你可以使用更多的工作阶段这里PHP Sessions

这里是first.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(); 

?> 
1
<?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(); 

?> 

代码first.php你甲肝保存$v1在会话变量.. 这样

<?php 
session_start(); // should be on top of your script 
$v1 = $_REQUEST['usn']; 
$_SESSION['v1'] = $v1; 
?> 
Second.php

可以使用$_SESSION['usn']

<?php 
session_start(); 
$v1 = $_SESSION['v1']; 
$q=mysql_query("select * from users where usn='$v1'"); 

?> 

注意:使用mysql_*不赞成..使用mysqli

0

作出config.php文件,把所有常用的变量和值有

然后require_once( 'config.php文件');在其他PHP文件

config.php 
<? 
$v =1; 
?> 

second.php 
<? 
require_once('config.php'); 
echo $v; 
?> 
相关问题