url上的PHP/JSON参数
我创建了一个php文件,它连接到数据库,从表中获取数据并以json格式显示 文件名为index.php 要查看json,我只需转到浏览器中的文件:url上的PHP/JSON参数,php,json,Php,Json,我创建了一个php文件,它连接到数据库,从表中获取数据并以json格式显示 文件名为index.php 要查看json,我只需转到浏览器中的文件: http://127.0.0.1/json/index.php and it displays: {"title":[{"id":"1","title":"Title1","desc":"Description1"},{"id":"2","title":"Title2","desc":"Description2"}]} 我需要做的是通过添加以下参
http://127.0.0.1/json/index.php and it displays:
{"title":[{"id":"1","title":"Title1","desc":"Description1"},{"id":"2","title":"Title2","desc":"Description2"}]}
我需要做的是通过添加以下参数来过滤:
For example: http://127.0.0.1/json/index.php?id=1 to just show the data with an id of 1 but it still shows all the data.
以下是php代码:
<?php
$username = "root";
$password = "";
$hostname = "localhost";
//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
$selected = mysql_select_db("mydb",$dbhandle)
or die("Could not select mydb");
$result = mysql_query("SELECT * FROM contacts");
$rows = array();
while($r = mysql_fetch_assoc($result)) {
$rows['title'][] = $r;
}
print json_encode($rows);
?>
我在这里做错了什么或遗漏了什么?您需要在查询中添加
where
条件
$id = (int) $_GET['id'];
$result = mysql_query("SELECT * FROM contacts WHERE id = $id");
您需要将
where
条件添加到查询中
$id = (int) $_GET['id'];
$result = mysql_query("SELECT * FROM contacts WHERE id = $id");
以下更改
$result = mysql_query("SELECT * FROM contacts");
到
跟随变化
$result = mysql_query("SELECT * FROM contacts");
到
首先,您必须将WHERE添加到SQL语句中
SELECT * FROM `contacts` WHERE `id` = $id
在您看到的id
中,这应该是表中id列的名称,不管它是什么。但是你也必须先对输入进行消毒
if(!is_numeric($_GET['id']))
exit; // if not a number then exit
$id = mysql_real_escape_string($_GET['id']); // escape the input
当然,这是最基本的错误检查。你可以解释一下。所以你的代码看起来更像这样
<?php
$username = "root";
$password = "";
$hostname = "localhost";
//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
$selected = mysql_select_db("mydb",$dbhandle)
or die("Could not select mydb");
if(!is_numeric($_GET['id']) || !$_GET['id'])
exit; // if not an integer or id not set then exit
$id = mysql_real_escape_string($_GET['id']); // escape the input
$result = mysql_query("SELECT * FROM contacts WHERE id = $id");
$rows = array();
while($r = mysql_fetch_assoc($result)) {
$rows['title'][] = $r;
}
print json_encode($rows);
?>
<?php
$username = "root";
$password = "";
$hostname = "localhost";
//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
$selected = mysql_select_db("mydb",$dbhandle)
or die("Could not select mydb");
$sql = "SELECT * FROM `contacts`";
if(isset($_GET['id']) && $_GET['id'] > 0) {
// if id is set then add the WHERE statement
if(!is_numeric($_GET['id']))
die('id must be an integer'); // if id is not an integer then exit
$id = mysql_real_escape_string((int)$_GET['id']); // escape the input
$sql .= " WHERE `id` = $id"; // append the WHERE statement to the sql
}
$result = mysql_query($sql);
$rows = array();
while($r = mysql_fetch_assoc($result)) {
$rows['title'][] = $r;
}
print json_encode($rows);
?>
首先,您必须将WHERE添加到SQL语句中
SELECT * FROM `contacts` WHERE `id` = $id
在您看到的id
中,这应该是表中id列的名称,不管它是什么。但是你也必须先对输入进行消毒
if(!is_numeric($_GET['id']))
exit; // if not a number then exit
$id = mysql_real_escape_string($_GET['id']); // escape the input
当然,这是最基本的错误检查。你可以解释一下。所以你的代码看起来更像这样
<?php
$username = "root";
$password = "";
$hostname = "localhost";
//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
$selected = mysql_select_db("mydb",$dbhandle)
or die("Could not select mydb");
if(!is_numeric($_GET['id']) || !$_GET['id'])
exit; // if not an integer or id not set then exit
$id = mysql_real_escape_string($_GET['id']); // escape the input
$result = mysql_query("SELECT * FROM contacts WHERE id = $id");
$rows = array();
while($r = mysql_fetch_assoc($result)) {
$rows['title'][] = $r;
}
print json_encode($rows);
?>
<?php
$username = "root";
$password = "";
$hostname = "localhost";
//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
$selected = mysql_select_db("mydb",$dbhandle)
or die("Could not select mydb");
$sql = "SELECT * FROM `contacts`";
if(isset($_GET['id']) && $_GET['id'] > 0) {
// if id is set then add the WHERE statement
if(!is_numeric($_GET['id']))
die('id must be an integer'); // if id is not an integer then exit
$id = mysql_real_escape_string((int)$_GET['id']); // escape the input
$sql .= " WHERE `id` = $id"; // append the WHERE statement to the sql
}
$result = mysql_query($sql);
$rows = array();
while($r = mysql_fetch_assoc($result)) {
$rows['title'][] = $r;
}
print json_encode($rows);
?>
请不要对新代码使用
mysql.*
函数。它们不再得到维护,社区已开始恢复。看到了吗?相反,你应该学习并使用或。如果你不能决定,试试看。如果你想学习,。请不要使用mysql.*
函数来编写新代码。它们不再得到维护,社区已开始恢复。看到了吗?相反,你应该学习并使用或。如果你不能决定,试试看。如果你想学习,。如果我在sql查询中硬编码id,我将被迫对其进行过滤。我需要它是可选的,这样我可以显示全部或通过url添加过滤器。获取错误:尖叫:错误抑制被忽略(!)注意:未定义的索引:在第16行的C:\wamp\www\json\index.php中的id,当我添加?id=1时,页面是空白的,您可能有严格的错误报告。现在尝试上面的代码。还必须使用is_numeric()而不是is_int()。如果我在sql查询中硬编码id,我将被迫对其进行筛选。我需要它是可选的,这样我可以显示全部或通过url添加过滤器。获取错误:尖叫:错误抑制被忽略(!)注意:未定义的索引:在第16行的C:\wamp\www\json\index.php中的id,当我添加?id=1时,页面是空白的,您可能有严格的错误报告。现在尝试上面的代码。还必须使用is_numeric()而不是is_int()。这几乎是完美的,但是如果我不进行过滤,我会得到一个错误。我需要的过滤是可选的这几乎是完美的,但我得到一个错误,如果我不做过滤。我需要过滤是可选的获取错误:尖叫:错误抑制忽略为。。。注意:未定义的变量:第23行的C:\wamp\www\json\index.php中的结果..这是第23行:while($r=mysql\u fetch\u assoc($result)){If在我最后添加?id=1时效果很好,但不显示任何内容,我不添加过滤器。如果我不过滤数据,我需要它只显示add数据。我应该保留哪个查询…$query=“从联系人
”中选择*还是另一个?将两者都保留,一个是设置了$id
,另一个是所有联系人。让我把整个代码做得很完美,但为什么会出现错误:注意:第13行的C:\wamp\www\json\index.php中未定义的索引:id——第13行是:$id=(int)$\u get['id'];获取错误:尖叫:忽略错误抑制…注意:未定义变量:第23行的C:\wamp\www\json\index.php中的结果。这是第23行:while($r=mysql\u fetch\u assoc($result)){如果在末尾添加?id=1时效果很好,但不显示任何内容,我不添加筛选器。如果不进行筛选,我需要它只显示添加数据。我应该保留哪个查询…$query=“SELECT*FROMcontacts
“还是另一个?把它们都留下,一个是如果设置了$id
,另一个是所有的。让我把整个代码都放在完美的位置,但为什么会出现错误:注意:第13行的C:\wamp\www\json\index.php中的未定义索引:id——第13行是:$id=(int)$\u get['id';