Fatal error: Call to a member function query() on a non-object in PHP

Fatal error: Call to a member function query() on a non-object in PHP

这个错误有很多问题。我已经阅读了所有这些内容,但其中 none 解决了我的问题。他们的问题完全不同,这就是我问的原因。

我收到此错误:Fatal error: Call to a member function query() on a non-object。 它在线 $conexion->query($consulta);。 我已经检查了连接对象 ($conexion) 是否为 null,它不是,它正确 filled 与连接。 我也尝试过自定义默认查询(以防查询不正确),但我仍然遇到问题。

那么,这是怎么回事?

这是我的代码:

<?php 

function conectar(){

    $data = include_once('configDB.php');
    $c = mysql_connect($data["server"], $data["user"], $data["pass"]);

    if ($c)
        return $c;
    else
        exit("fail");
}

function insertar($nombre, $resultados, $tiempo){

    $conexion = conectar();

    $consulta = "INSERT INTO juegopreguntas (nombre, p1, p2, p3, p4, p5, tiempo) VALUES 
('".$nombre."',".$resultados[0].",".$resultados[1].",".$resultados[2].",".$resultados[3]
.",".$resultados[4].",'".$tiempo."')";

    $conexion->query($consulta);

    cerrarConexion($conexion);

}

function cerrarConexion($conexion){

    mysql_close($conexion);
}
?>

你应该select连接数据库

<?php 

function conectar(){

    $data = include_once('configDB.php');
    $c = mysql_connect($data["server"], $data["user"], $data["pass"], $data['database']);

    if ($c)
        return $c;
    else
        exit("fail");
}

function insertar($nombre, $resultados, $tiempo){

    $conexion = conectar();

    $consulta = "INSERT INTO juegopreguntas (nombre, p1, p2, p3, p4, p5, tiempo) VALUES 
('".$nombre."',".$resultados[0].",".$resultados[1].",".$resultados[2].",".$resultados[3]
.",".$resultados[4].",'".$tiempo."')";

    $query = mysql_query($consulta);

    cerrarConexion($conexion);

}

function cerrarConexion($conexion){

    mysql_close($conexion);
}
?>