我正在嘗試使用PDO連接在我的SQL記錄中查找最小值。記錄是varchar,所以必須將其轉換爲int才能找到最小值。我被困在這個問題上:PHP SQL:使用PDO查找最小值
mysql_fetch_assoc() expects parameter 1 to be resource, array given
問題是我不知道如何從PDO連接獲取資源。該查詢是有效的。
<?php
//load and connect
require("config.inc.php");
//change varcar to ints and put into array
$query = "SELECT score FROM easy";
$stmt = $db->prepare($query);
$result = $stmt->execute();
$rows = $stmt->fetchAll();
$scoreArray = array();
$index = 0;
while($row = mysql_fetch_assoc($rows)){
$scoreArray[$index] = intval($row);
$index++;
}
$smallest = min($scoreArray);
$response["success"] = 0;
$response["message"] = "The min is: ".$smallest;
echo(json_encode($response));
?>
您需要使用不PDO但** ** SQL。因爲它正是SQL正好爲 –
** mysql_fetch_assoc **與舊的折舊** mysql **函數有關。它與** PDO **無關。 – Subin