Skip to main content
Participant
October 22, 2015
Answered

I'm trying to pull a record set and I'd like to load the data into an array

  • October 22, 2015
  • 2 replies
  • 689 views

It loops through and I see it going through all the data correctly, but when I try to assign it to an array using $i - it just keeps replacing it, it doesn't add a new value to the array.

Thanks in advance!!

$index = "";
$service_singular = "";
$service_plural= "";
$sub_service = "";
$description = "";
$duration = "";
$duration_descript = "";
$price = "";
$i = 0;
if ($fac_id != "" && $fac_id != 0){
   if ($stmt = $mysqli->prepare("SELECT `index`,`service_singular`, `service_plural`, `sub_service`, `description`, `duration`, `duration_descript`, `price` FROM `service` WHERE `fac_id`= ?")) {
      $stmt->bind_param('s', $fac_id); // Bind "fac_id" to parameter.
      $stmt->execute(); // Execute the prepared query.
      $stmt->store_result();
      // $i = $stmt->num_rows;
   $stmt->bind_result($index,$service_singular, $service_plural, $sub_service, $description, $duration, $duration_descript, $price); // get variables from result.
     while ($stmt->fetch()){

         $i = $i++; }

   }

    This topic has been closed for replies.
    Correct answer ncsunderland

    Thanks for looking at it.

    ~~~~~~~~~~~~

    ); // get variables from result.

       while ($service->fetch()){

              $srv_rows[] = array("index"=>$index,"m_serve"=>$m_serve,"sub_service"=>$sub_service,"description"=>$description, "duration"=>$duration, "duration_descript"=>$duration_descript, "price"=>$price, "remove"=>$remove, "display"=>$display);

    }

    ~~~~~~~~~~~~~~

    This gave me what I needed.

    2 replies

    David_Powers
    Inspiring
    October 23, 2015

    It's not clear to me what you're trying to do. The code that you've posted gets only one record, and you're binding the results to variables. Is this part of an outer loop that's retrieving several records depending on fac_id? If so, initialize the variables as empty arrays.

    $index = [];

    $service_singular = [];

    $service_plural= [];

    $sub_service = [];

    $description = [];

    $duration = [];

    $duration_descript = [];

    $price = [];


    Then assign the result to the relevant array:


    $stmt->fetch();


    $index[] = $index;

    $service_singular[] = $service_singular; // and so on

    David_Powers
    Inspiring
    October 24, 2015

    I've been thinking about this a bit more. If you want an array of all the results, it's extremely simple.

    Build a comma-separated list of the record IDs using implode(), and then build a SQL query like this (I've used actual numbers to illustrate what the query should look like):

    $sql = 'SELECT * FROM service WHERE fac_id IN (1,3,7,12,21,25,26)';

    $result = $mysqli->query($sql);

    $all = $result->fetch_all(MYSQLI_ASSOC);

    This will create a multidimensional array with all the results in this format: $all[0]['fac_id'].

    One caveat, though. Storing everything in an array consumes a lot of memory, which could slow the server down, or even run out of memory on a busy shared server. Ideally, you should process database results as received.

    ncsunderlandAuthorCorrect answer
    Participant
    October 26, 2015

    Thanks for looking at it.

    ~~~~~~~~~~~~

    ); // get variables from result.

       while ($service->fetch()){

              $srv_rows[] = array("index"=>$index,"m_serve"=>$m_serve,"sub_service"=>$sub_service,"description"=>$description, "duration"=>$duration, "duration_descript"=>$duration_descript, "price"=>$price, "remove"=>$remove, "display"=>$display);

    }

    ~~~~~~~~~~~~~~

    This gave me what I needed.

    Rob Hecker2
    Legend
    October 23, 2015

    The following code doesn't really do anything:

    while ($stmt->fetch()){

             $i = $i++; }

    I don't use mySQLi, so I may not be much help for you, but do you really want to create an array of the query results? Or do you just want to loop through them? Ultimately, what are you trying to achieve.

    Sometimes one does want to create an array of the recordset. In PDO this is straightforward. There must be a similar method with mySQLi. But I wonder if that is what you need to do.

    Participant
    October 23, 2015

    I tried to create an array - $index[$i] - and load into that, but it was still an array of one and ended in the last instance.

    I need a full array.

    Rob Hecker2
    Legend
    October 23, 2015

    PHP Solutions by David Powers is a good beginner book on PHP.