[英]How to get products from table with exact ids from array?
I have a sql table with products and i don't get how to retrieve several products using array with their ids 我有一个包含产品的sql表,但我不知道如何使用具有其ID的数组来检索多个产品
i know how retrieve all of these products and only one using id 我知道如何检索所有这些产品,只有一个使用ID
JSON with all products from sql JSON与SQL中的所有产品
function loadGoods(){
$conn = connect();
$sql = "SELECT * FROM PRODUCTS";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
$out = array();
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
$out[$row["id"]] = $row;
}
echo json_encode($out);
} else {
echo 0;
}
mysqli_close($conn);
}
JSON with one product using its id received in js file from hash 通过哈希从js文件中接收到的带有产品ID的JSON产品
function loadSingleGoods(){
$id = $_POST['id'];
$conn = connect();
$sql = "SELECT * FROM PRODUCTS WHERE id='$id'";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
$row = mysqli_fetch_assoc($result);
echo json_encode($row);
} else {
echo 0;
}
}
Both of these snippets are working and now i have array with id's but i don't get how retrieve only their properties from sql in JSON file. 这两个代码片段都可以正常工作,现在我有了具有ID的数组,但是我不知道如何从JSON文件中的SQL中仅检索其属性。
I have array with ids like this ["6", "7", "27"] and i need to retrieve from sql PRODUCTS only items with these ids. 我有ID像这样的数组[“ 6”,“ 7”,“ 27”],我只需要从sql PRODUCTS中检索具有这些ID的项目。 As i understand i have to do something with this line
据我了解,我必须对此行做些事情
$sql = "SELECT * FROM PRODUCTS WHERE id='$id'";
i tried such code 我试过这样的代码
$sql = "SELECT * FROM PRODUCTS WHERE in_array("id", $ids, TRUE);
but it didn't work 但这没用
It would be helpful if you showed us your JavaScript also, but assuming you're feeding the output of the json_encode()
into a single variable, you can access each product like so: 如果您也向我们展示了JavaScript,这将很有帮助,但是假设您将
json_encode()
的输出提供给单个变量,则可以像这样访问每个产品:
console.log(json_variable[0]);
That should give you the first "row". 那应该给你第一个“行”。 To access a particular cell, try this:
要访问特定的单元格,请尝试以下操作:
console.log(json_variable[0]['column_name']);
To go thru all of them, try jQuery's each()
: 要遍历所有这些,请尝试使用jQuery的
each()
:
$.each( json_variable, function( id, column_name) {
console.log( id + ": " + column_name);
});
You can parse json object using JSON.parse()
and access all the value of the object. 您可以使用
JSON.parse()
解析json对象并访问该对象的所有值。
var text = '{ "name":"John", "age":"39", "city":"New York"}';
JSON.parse(text, function (key, value) {
console.log(value);
});
Here you can replace text
with your json
variable. 在这里,您可以将
text
替换为json
变量。
If you want to read more about json parse & how to access json variable in js click here . 如果您想了解有关json解析以及如何在js中访问json变量的更多信息, 请单击此处 。
You can build your query before executing it: 您可以在执行查询之前构建查询:
$sql_array=array("6", "7", "27");
$first_item=array_values($sql_array)[0];
$sql="SELECT * FROM `PRODUCTS` WHERE `id`='$first_item'";
$built_query="";
foreach ($sql_array as $k => $id) {
if ($k < 1) continue;
$built_query=$built_query." OR `id` = '$id'";
}
$built_query=$sql.$built_query;
echo "$built_query";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.