Php Json和Mysql问题

Php Json和Mysql问题,php,mysql,json,Php,Mysql,Json,下面是我的json php代码 include("connect.php"); $id = $_GET['lid']; function countRec($fname,$tname) { $sql = "SELECT * FROM `mail` WHERE confirmed = 'no' AND label_id = '". $id ."'"; $result = mysql_query($sql) or die ('test'); $num = mysql_num_rows($resu

下面是我的json php代码

include("connect.php"); 
$id = $_GET['lid'];
function countRec($fname,$tname) {
$sql = "SELECT * FROM `mail` WHERE confirmed = 'no' AND label_id = '". $id ."'";
$result = mysql_query($sql) or die ('test'); 
$num = mysql_num_rows($result);
return $num;
}

$page = $_POST['page'];
$rp = $_POST['rp'];
$sortname = $_POST['sortname'];
$sortorder = $_POST['sortorder'];

if (!$sortname) $sortname = 'ID';
if (!$sortorder) $sortorder = 'desc';

    $sort = "ORDER BY $sortname $sortorder";

if (!$page) $page = 1;
if (!$rp) $rp = 10;

$start = (($page-1) * $rp);

$limit = "LIMIT $start, $rp";

$sql = "SELECT * FROM `mail` WHERE confirmed = 'no' AND label_id = '". $id ."' $sort $limit";
$result = mysql_query($sql) or die ('test'); 

$total = countRec();

header("Expires: Mon, 26 Jul 1997 05:00:00 GMT" );
header("Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . "GMT" );
header("Cache-Control: no-cache, must-revalidate" );
header("Pragma: no-cache" );
header("Content-type: text/x-json");
$json = "";
$json .= "{\n";
$json .= "page: $page,\n";
$json .= "total: $total,\n";
$json .= "rows: [";
$rc = false;
while ($row = mysql_fetch_array($result)) {
if ($rc) $json .= ",";
$json .= "\n{";
$json .= "id:'".$row['ID']."',";
$json .= "cell:['".$row['email']."'";
$json .= ",'".addslashes($row['name'])."'";
$json .= ",'".addslashes($row['country'])."'";
$json .= ",'".addslashes($row['bus'])."'";
$json .= ",'".addslashes($row['website'])."'";
$json .= ",'".addslashes($row['music'])."'";
$json .= ",'".addslashes($row['radio'])."']";
$json .= "}";
$rc = true;
}
$json .= "]\n";
$json .= "}";
echo $json;
我将数据发布到这个php,如“req.php?lid=3434”

并且获得像
$id=$\u GET['lid']这样的“lid”如您所见

但是在我的mysql中,当我写
时,label_id='$id'
就不起作用了

有什么建议吗


谢谢

您正在函数中引用全局
$id
。您需要将其标记为全局:

function countRec($fname,$tname) {
    global $id;
    //etc
}
或者可以将其作为第三个参数传递给函数,这可能是更好的解决方案


请注意,此代码容易受到SQL注入攻击。您应该引用
$id
(例如using),或者如果它总是一个整数,您可以将其强制转换为整数,例如
$id=(int)$id
。更好的是,您可以使用和,这样就消除了这个问题。

您正在引用函数中的全局
$id
。您需要将其标记为全局:

function countRec($fname,$tname) {
    global $id;
    //etc
}
或者可以将其作为第三个参数传递给函数,这可能是更好的解决方案


请注意,此代码容易受到SQL注入攻击。您应该引用
$id
(例如using),或者如果它总是一个整数,您可以将其强制转换为整数,例如
$id=(int)$id
。更好的是,您可以使用和,这就解决了这个问题。

我将
$id
作为第三个参数,而不是使用
global
。我将
$id
作为第三个参数,而不是使用
global