Tuesday 28 August 2018

MySQLI query Bind_param does not return results

I am trying to get this function I wrote to return some data from a MySQL table. Here is my function.

function getCompInfoIDS($id) {
    global $mysqli;
    $query = "
Select
  computers.asset,
  computers.serial,
  rooms.building_id,
  rooms.id,
  computers.assigned_person,
  computers.computer_name,
  computers.sticker,
  operating_systems.manufacturer_id,
  operating_systems.id As id1,
  computers.memory,
  computers.hard_drive,
  computers.department,
  computers.year_purchased,
  computers.po_cs_ca,
  computers.mac_address,
  computers.group_id,
  models.manufacturer_id As manufacturer_id1,
  models.id As id2,
  computers.type,
  computers.monitor_size
From
  computers Inner Join
  rooms On computers.room_id = rooms.id Inner Join
  operating_systems On computers.os_id = operating_systems.id Inner Join
  models On computers.model = models.id
 Where
  computers.id=?";
    $stmt = $mysqli -> prepare($query);
    $stmt -> bind_param('i',$id);
    $stmt -> execute();
    $stmt -> bind_result($asset, $serial, $building_id, $room_id, $assigned_person, $computer_name, $sticker, $os_type_id, $os_id, $memory, $hard_drive, $department, $year_purchased, $po_cs_ca, $mac_address, $group_id, $model_type_id, $model_id, $comp_type, $monitor_size, $date_modified);
    while($stmt -> fetch()){
        $computer_info = array($asset, $serial, $building_id, $room_id, $assigned_person, $computer_name, $sticker, $os_type_id, $os_id, $memory, $hard_drive, $department, $year_purchased, $po_cs_ca, $mac_address, $group_id, $model_type_id, $model_id, $comp_type, $monitor_size, $date_modified);
    }

    return $computer_info;

The query does work and I have tested it with multiple ids in phpmyadmin.
I have been following the PHP manual, and this site to do step by step to see what I am doing wrong. I have done echo "test"; at various different spots in my code to see where the function is failing and I cannot find a single point of failure in the function, it's just not filling the array with results.
I tried doing echo $asset; right before I fill the array and nothing is shown so I don't believe that any data is actually being put into the array variables.

You must add $stmt->store_result();, so your code is like this :
$stmt = $mysqli -> prepare($query);
$stmt -> bind_param('i',$id);
$stmt -> execute();
$stmt->store_result();
$stmt -> bind_result($asset, $serial, $building_id, $room_id, $assigned_person, $computer_name, $sticker, $os_type_id, $os_id, $memory, $hard_drive, $department, $year_purchased, $po_cs_ca, $mac_address, $group_id, $model_type_id, $model_id, $comp_type, $monitor_size, $date_modified);
while($stmt -> fetch()){
    $computer_info = array($asset, $serial, $building_id, $room_id, $assigned_person, $computer_name, $sticker, $os_type_id, $os_id, $memory, $hard_drive, $department, $year_purchased, $po_cs_ca, $mac_address, $group_id, $model_type_id, $model_id, $comp_type, $monitor_size, $date_modified);
}

0 comments:

Post a Comment