简体   繁体   中英

mysqli sub query with prepared statement returning boolean

so i prepared a query that already works in mysql( tested in phpmyadmin ) but I have problem getting the results using mysqli class and its really weird not to get the results because I believe that the query is correct. Can anyone point out my mistake in the following code.

<?php

include_once __DIR__ . '/includes/init.php';

$sql = 'SELECT 
                    `purchase_order`.`id`, 
                    `purchase_order`.`po_date` AS po_date, 
                    `purchase_order`.`po_number`, 
                    `purchase_order`.`customer_id` AS customer_id , 
                    `customer`.`name` AS customer_name, 
                    `purchase_order`.`status` AS po_status, 
                    `purchase_order_items`.`product_id`, 
                    `purchase_order_items`.`po_item_name`, 
                    `product`.`weight` as product_weight,
                    `product`.`pending` as product_pending,
                    `product`.`company_owner` as company_owner,
                    `purchase_order_items`.`uom`, 
                    `purchase_order_items`.`po_item_type`, 
                    `purchase_order_items`.`order_sequence`, 
                    `purchase_order_items`.`pending_balance`, 
                    `purchase_order_items`.`quantity`, 
                    `purchase_order_items`.`notes`, 
                    `purchase_order_items`.`status` AS po_item_status,
                    `purchase_order_items`.`id` AS po_item_id
                  FROM (SELECT id, po_date, po_number, customer_id, status
            FROM purchase_order  GROUP BY  id   DESC  LIMIT ?, ? ) as purchase_order  
                  INNER JOIN customer ON `customer`.`id` = `purchase_order`.`customer_id`  
                  INNER JOIN purchase_order_items ON `purchase_order_items`.`po_id` = `purchase_order`.`id` 
                  INNER JOIN product ON `purchase_order_items`.`product_id` = `product`.`id` 
                   GROUP BY  id';

$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
$statement = $mysqli->prepare($sql);
$statement->execute();
$offset = 0;
$perpage = 10;
$statement->bind_param('ii', $offset, $perpage);
$result = $statement->get_result();
while ($row = $result->fetch_assoc())
{
    echo '<pre>';
    print_r($row);
    echo '</pre>';
}

Thanks in advance.

The sequence in which you prepare/bind and execute the statement and values is incorrect...

$statement = $mysqli->prepare($sql);
$offset = 0;
$perpage = 10;
$statement->bind_param('ii', $offset, $perpage);
$statement->execute();

You need to use bind_param() before the execute() so that it knows what values to use.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM