Is there a "mysqli_fetch_all" using a column (PK) as an index in the resulting array?

I currently have something for this:

<?php

  // ...More Code

  $result = mysqli_query($mysqli, "SELECT * FROM stock_types");
  if(!$result || mysqli_num_rows($result) < 1) die('error');
  $stock_types = mysqli_fetch_all($result, MYSQLI_ASSOC);
  mysqli_free_result($result);
  die(print_r($stock_types,true));

  // More Code...

?>

Will put something into action:

Array (
  [0] => Array (
    [type_id] => 1
    [type_name] => In Stock
    [type_visible] => 1
    [type_locked] => 0
  )
  [1] => Array (
    [type_id] => 2
    [type_name] => Out of Stock
    [type_visible] => 1
    [type_locked] => 1
  )
  [2] => Array (
    [type_id] => 3
    [type_name] => Offline
    [type_visible] => 0
    [type_locked] => 1
  )
  [3] => Array (
    [type_id] => 5
    [type_name] => Hidden
    [type_visible] => 0
    [type_locked] => 0
  )
)

Is there a mysqli fetch filter (correct term?) That will use the primary key from the result set if it exists as an array index value? In case my question is not clear, this will lead to something, and not to its effect:

Array (
  [1] => Array (
    [type_name] => In Stock
    [type_visible] => 1
    [type_locked] => 0
  )
  [2] => Array (
    [type_name] => Out of Stock
    [type_visible] => 1
    [type_locked] => 1
  )
  [3] => Array (
    [type_name] => Offline
    [type_visible] => 0
    [type_locked] => 1
  )
  [5] => Array (
    [type_name] => Hidden
    [type_visible] => 0
    [type_locked] => 0
  )
)

It doesn't matter if the values type_idare deleted, but it would be very useful to use the primary key to index the array. I can do this with a fetch loop, but I'm wondering if there is a simpler and more elegant way to handle this.

+4
source share
3

ext/mysqli . , , :

, - :

$set = array();
while ($row = $result->fetch_assoc()) {
    $set[array_shift($row)] = $row;
}

fetch_all(), :

$set = array();
$data = $result->fetch_all(); 
while ($row = array_shift($data)) {
    $set[array_shift($row)] = $row;
}
+3

, , php-. , 2.

Array
(
    [0] => Array
        (
            [id] => 1
            [name] => xxx
        )

    [1] => Array
        (
            [id] => 2
            [name] => yyy
        )
)

, , : array_column

Array
(
    [1] => xxx
    [2] => yyy
)

:

$result_set = mysqli_fetch_all($restult_set , MYSQLI_ASSOC);
//syntax: array_column(arg, column_key, index_key)
$result_set = array_column($result_set, 'name' , 'id');

, , mysqli mysqli_fetch . .

+2

, - . PHP . , mysqli. , .

, : :

$stock_types = $db->getInd('type_id', "SELECT * FROM stock_types");
print_r($stock_types);die;

. , , mysqli (, , mysqli, ?).

, :

$data = $db->getAll("SELECT * FROM ?n WHERE id IN (?a) LIMIT ?i",$table,$ids,$limit);

This will lead you to an insane amount of raw mysqli code if you do it correctly and safely. While with a correctly written abstraction library there is still no more than one line

0
source

Source: https://habr.com/ru/post/1534076/


All Articles