1
$stmt = ("SELECT FROM_UNIXTIME(`stock`.`created_at`, '%Y-%m-%d') as date,stock.pid,stock.name,stock.description,stock.price,stock.final_price as cost, FROM `stock` WHERE stock.type = 'partbike' AND stock.store = 'TWN'");

$query = mysqli_query($db,$stmt);

 
// // Fetch records from database 
// $query = $db->query("); 
 
if($query->num_rows > 0){ 
    $delimiter = ","; 
    $filename = "stock" . date('Y-m-d') . ".csv"; 
     
    // Create a file pointer 
    $f = fopen('php://memory', 'w'); 
     
    // Set column headers 
    $fields = array('Date','Part.ID','Make','Model','Price','Cost'); 
    fputcsv($f, $fields, $delimiter); 
     
    // Output each row of the data, format line as csv and write to file pointer 
    while($row = $query->fetch_assoc()){ 
        // $status = ($row['status'] == 1)?'Active':'Inactive'; 
        $lineData = array($row['date'], $row['pid'], $row['name'], $row['description'], $row['price'], $row['cost']); 
        fputcsv($f, $lineData, $delimiter); 
    } 

The if statement is meant to check if the number of data collected if above zero to avoid errors of null values in the future. Anyone with an idea why am getting that error?

  • 3
    Your query is failing. Check for errors after you execute it to find out why. – Tangentially Perpendicular Oct 31 '22 at 09:22
  • 2
    It'll be because, as the error says, `$query` is boolean (true/false) and you can't call a function on a true/false value. And if you [read the manual](https://www.php.net/manual/en/mysqli.query.php) you'll see mysqli_query returns false when the SQL crashes. – ADyson Oct 31 '22 at 09:23
  • 1
    Add `mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);` before your `mysqli_connect()` (or `new mysqli()`) command, and this will ensure that errors with your SQL queries are reported correctly to PHP automatically, and then you'll get a crash report and be able to see what the underlying problem is. – ADyson Oct 31 '22 at 09:24
  • It's closed but the answer is you have a syntax error, it's `$query->num_rows() > 0` – Forbs Nov 01 '22 at 01:00

0 Answers0