Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call to a member function query() on a non-object

Tags:

php

mysqli

I get this error:

Call to a member function query() on a non-object.

Also when I use other MySQLi functions it gives me the same error but for that function.

My connection.php:

<?php
$mysqli = new mysqli("localhost","user","pass","db");
if(mysqli_connect_errno()){
  trigger_error('Connection failed: '.$mysqli->error);
}

?>

My function:

function form_year($find, $value, $from, $vehicletype){
    //Query setup
    $autoquery = "SELECT    
                        Year, 
                        Vehicle, 
                        Brand, 
                        Model, 
                        Type, 
                    FROM 
                        vehicle_tbl
                    WHERE
                        Vehicle = '".$vehicletype."'
                    GROUP BY
                        Year";
    $autoresult = $mysqli->query($autoquery);

    $search = array("%value%", "%options%");
    $rows = file_get_contents("tpl/options.html");
    while($row = $autoresult->fetch_assoc()){
        $replace = array($row[$value], $row[$value]);
        $tr .= str_replace($search, $replace, $rows);
    }
    return str_replace($find, $tr, $from);
}

Function and connection is included where the function is called

like image 848
Sinan Samet Avatar asked Dec 19 '25 01:12

Sinan Samet


1 Answers

Quick/dirty fix:

  function form_year($find, $value, $from, $vehicletype){
    //Query setup
    global $mysqli;
    $autoquery = "SELECT  [...]
like image 69
Green Black Avatar answered Dec 21 '25 15:12

Green Black



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!