我正在尝试获取我写的这个函数来从MySQL表中返回一些数据。这是我的功能。
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;
查询确实有效,我在phpmyadmin中使用多个ID对其进行了测试。
我一直在关注PHP manual和this site,一步一步地看看我做错了什么。我已经在我的代码中的各个不同位置完成echo "test";
以查看函数失败的位置,并且我在函数中找不到单点故障,它只是没有用结果填充数组。
我在填充数组之前尝试做echo $asset;
并且没有显示任何内容,所以我不相信任何数据实际上都被放入数组变量中。
答案 0 :(得分:1)
您必须添加$stmt->store_result();
,因此您的代码如下:
$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);
}
答案 1 :(得分:0)
您的代码应该像这样给出错误:
Warning: mysqli_stmt::bind_result(): Number of bind variables doesn't match number of fields in prepared statement in C:\xampp\htdocs\... on line x
查询中所选列的数量与绑定结果的数量不同。您在SELECT
查询中只选择了20列,但您的bind_result()
中有21个变量。
您的查询中必须缺少一列。显然是你的$date_modified
变量。正确填写SELECT
查询,以便绑定$date_modified
变量。