Showing posts with label Mysqli BIND PARAM. Show all posts
Showing posts with label Mysqli BIND PARAM. Show all posts

Tuesday, 4 September 2018

The MySQL query in PHP does not work correctly, is there a way to get the exact query?

Is there a way to get the query after MySQLi prepares it? My query is messing up:

$query = "UPDATE event SET group=?, boxed=?, name=?, location=?, time=?, day=?, type=? WHERE id=? LIMIT 1";
if($stmt = $db -> prepare($query))
{
    $stmt -> bind_param("iisssssi", $group, $boxed, $name, $location, $time, $day, $etype, $id);
    $stmt -> execute();
    $stmt -> close();
}
else

Error message:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'group=?, boxed=?, name=?, location=?, time=?, day=?, type=? WHERE id=? LIMIT 1' at line 1
Variable group is an int, boxed is an int, the rest are strings, and id is an int.

You're using the SQL reserved word "group" as one of the column names.

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);
}