のプロパティを取得しようとするにあたっては、私がVaRのダンプを行うと、すべての正しい値が表示され</p> <p> JSONやPHPを使用して、特定のIDのデータベースからの値を表示しようとすると、非オブジェクトエラー
function getItemByID($id) {
//Connect to database and server
include ("connect.php") ;
if (!$connect) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT * FROM items WHERE id ='".$id."'";
$res1 = mysqli_query($connect, $sql);
$rows = mysqli_fetch_assoc($res1);
$itemtxt = '[{"id" : '.$rows['id'].',' ;
$itemtxt = $itemtxt.'"item" : '.$rows['item'].',' ;
$itemtxt = $itemtxt.'"image" : '.$rows['image'].',' ;
$itemtxt = $itemtxt.'"descr" : '.$rows['descr'].'}] ,' ;
$itemtxt = substr($itemtxt, 0, -2) ;
var_dump($itemtxt);
return $itemtxt;
}
エラーがライン上から表示されます
これがどこにある10,11,12,13
<?php
$id = $_GET['id'];
include("library.php");
$itemtxt = getItemByID($id);
$itemjson = json_decode($itemtxt);
echo $itemjson -> id;
echo $itemjson -> item;
echo $itemjson -> image;
echo $itemjson -> descr;
?>
このデータをこのページに表示しようとしていますが、「非オブジェクトのプロパティを取得しようとしています」というエラーが表示されます。
を、あなたは何をすべきか、出力だから、以下の変更'var_dump($ itemtxt);から取得しますか? –
私はこれを手に入れましょう...あなたがエンコードしている 'getItemByID'では**(手で!)**、後でデコードできますか?手btwでエンコードせず、 'json_decode'に' json_encode'という名前の小さな姉妹がいます。もっと良いことに、 '$ rows'変数を返すだけで、エンコード/デコードをスキップします – YvesLeBorg