[英]How to fetch data into JSON encode array in PHP?
I am trying to fetch some column values (item, description, price etc) into these json_encode
arrays.我正在尝试将一些列值(项目、描述、价格等)提取到这些json_encode
数组中。
Inside the arrays you can see the 'amount' string is the one I have attempted on.在数组中,您可以看到 'amount' 字符串是我尝试过的字符串。
I tried using $value = $fetch['price'];
我尝试使用$value = $fetch['price'];
and then inside the array using $value
but it doesn't accept it.然后在数组内部使用$value
但它不接受它。
I also tried..我也试过了。。
'amount' => ['price']
&
'amount' => "$fetch['price']"
&
'amount' => `$fetch['price']`
&
'amount' => {$fetch['price']}
I saw some other threads on stackoverflow showing that the fetch query maybe needs to be inside the json_encode
?我在 stackoverflow 上看到其他一些线程显示 fetch 查询可能需要在json_encode
? but that was too fetch all results and encode them whereas I already have the encoding so I'm not sure.但这太获取所有结果并对其进行编码,而我已经有了编码,所以我不确定。
Any help will be appreciated, thanks :)任何帮助将不胜感激,谢谢:)
$userdetails = $_SESSION[usr_name];
$stmt = $dbh->prepare("SELECT * FROM `products` WHERE `username` = '$userdetails' ORDER BY `uid` DESC");
$stmt->execute();
$fetch = $stmt->fetchAll();
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.commerce.coinbase.com/charges');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(
array (
'name' => 'My-Business-Name',
'description' => "Selected Product: ",
'local_price' =>
array (
'amount' => $fetch['price'],
'currency' => 'GBP',
),
'pricing_type' => 'fixed_price',
'metadata' =>
array (
'customer_id' => 'uid_1',
'customer_name' => 'Satoshi Nakamoto',
)
));
$result = curl_exec($ch);
curl_close($ch);
$response = json_decode($result, true);
Lets do small refactoring and typo fixes让我们做一些小的重构和错别字修复
$userdetails = $_SESSION[usr_name];
It should be either $_SESSION['usr_name']
or $_SESSION[$usr_name]
.它应该是$_SESSION['usr_name']
或$_SESSION[$usr_name]
。
Next thing下一件事
$stmt = $dbh->prepare("SELECT * FROM `products` WHERE `username` = '$userdetails' ORDER BY `uid` DESC");
The correct and safer way to use prepared statements is next( according to php pdo docs )使用准备好的语句的正确和更安全的方法是下一个(根据 php pdo docs )
$stmt = $dbh->prepare("SELECT * FROM `products` WHERE `username` = :username ORDER BY `uid` DESC");
$stmt->execute(array( ':username' => $userdetails ));
$fetch = $stmt->fetchAll();
After this do something like var_dump or print_r on $fetch
just to be sure that data is there as brombeer said.在此之后,在$fetch
上执行诸如var_dump或print_r 之类的操作,以确保数据如 brombeer 所说的那样存在。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.