我是PHP新手,我需要管理数组的帮助。下面的代码是从我的SQL数据库中提取数据。我需要知道如何提取$resultArray
中的值。我坚信$resultArray
是一个关联数组,但我不确定如何获取值。
我试过$result = $resultArray['ImageID'];
但它不起作用。我确定它会通过在我的 Xcode 上打印出来来检索一些东西。
public function GetImage($imageurl)
{
$returnValue = array();
$sql = "SELECT ImageID FROM `ImageTable` WHERE ImageURL = '$imageurl'";
$result = $this->conn->query($sql);
$resultArray = array();
$tempArray = array();
if ($result != null && (mysqli_num_rows($result) >= 1)) {
while ($row = $result->fetch_object()) {
$tempArray = $row;
array_push($resultArray, $tempArray);
}
}
return $resultArray;
}
我不太确定您是否正在使用mysqli
,但如果您这样做,那么这里有一个答案:
$conn = new mysqli(...); // Your connection to the database
$result = $conn->query("SELECT * FROM `ImageTable` WHERE ImageURL = '$imageurl'"); // Query and get $result
while($row = $result->fetch_assoc()) // Extract rows (even if only one is returned)
{
echo $row["ImageID"]; // Echoes "ImageID"-value of this row.
// ...
}
希望它有帮助。