Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
257 views
Welcome To Ask or Share your Answers For Others

1 Answer

Quick/dirty fix:

  function form_year($find, $value, $from, $vehicletype){
    //Query setup
    global $mysqli;
    $autoquery = "SELECT  [...]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...