使用PHP和MYSQL将json_encode用于URL

时间:2011-08-12 21:42:36

标签: php mysql arrays json

我需要检索来自PHP文件的URL,其中包含mysql变量。不幸的是他们没有被正确归还。

以下是我链接的html文件:

<!doctype html>
<html>
<head>
  <meta charset="utf-8">
  <link rel="index" href="toc1.php" type="application/json">
</head>

我的toc1.php是文件:

<?php
  $username = $_GET['username'];
  $papername = $_GET['papername'];

  header('Content-Type: application/json');
  $username = json_encode($username);
  $papername = json_encode($papername);
?>

[{
"url": <?php echo '<a href="http://www.yoozpaper.com/cover.php?
username=' . $username . '&papername=' . $papername . '" ></a>';?>
},
{
"url": <?php echo '<a href="http://www.yoozpaper.com/tocindex.php?
username=' . $username . '&papername=' . $papername . '" ></a>';?>
},]

4 个答案:

答案 0 :(得分:1)

如何删除json_encode?

json_encode函数使json对象形成数组:

$json = array();
$json['something'] = "something else";
$json['and_again'] = "more things";

然后json_encode($json)返回:

{
     "something": "something",
     "and_again": "things"
}

所以json编码一个字符串:

$username = "John Doe";
echo json_encode($username);

会导致"John Doe"或错误

答案 1 :(得分:0)

我建议遵循:

$list = array();

$objItem = new stdClass();
$objItem->url = '<a href="…?username=' . $username . '&papername=' . $papername . '"></a>';
$list[] = $objItem;

// add more items

最后,输出:

echo json_encode($list);

答案 2 :(得分:0)

header()调用必须是文件的第一行,请参阅manual。所以把它改成

<?php
header('Content-Type: application/json');

$username = $_GET['username'];
$papername = $_GET['papername'];

$username = json_encode($username);
$papername = json_encode($papername);
?>

GET变量应该来自哪里?您可能想要将链接更改为

<link rel="index" href="toc1.php?username=foo&papername=bar" type="application/json">

答案 3 :(得分:0)

您的Json {"some":"some"}

此处href =''

<a href='{"some":"some"}'>Some</a>

不是这个

<a href="{"some":"some"}">Some</a>