0

I'm new to php/mysql and have run into this problem.

My code works, but the $sqlResponse gives out an array. In the example below (the current response) I only want '117'.

"sortingId":{"@last_sortingId := MAX(sortingId)":"117"}

My code is:

$sqlResponseGet = mysqli_query($this->db, "SELECT @last_sortingId := MAX(sortingId) FROM items");
$sqlResponse = mysqli_fetch_assoc($sqlResponseGet);

$this->response($this->json(array('sortingId' => $sqlResponse)), 200);

I tried various things, like $sqlResponseResult = $sqlResponse[0] and The answer from this Single Value Mysqli

But can't get it to work

Really hope you can help me, thanks :)

Jonas Borneland
  • 383
  • 1
  • 6
  • 19

1 Answers1

3

The first thing you should do is give an alias to the selected column:

$sqlResponseGet = mysqli_query($this->db, "SELECT @last_sortingId := MAX(sortingId) AS last_sortingId FROM items");

The result of mysqli_fetch_assoc() will be an associative array with this as the key, so you should index it.

$this->response($this->json(array('sortingId' => $sqlResponse['last_sortingId'])), 200);
Barmar
  • 741,623
  • 53
  • 500
  • 612