";没有选择数据库“;只是在第二个和下面的查询-PHP-MySQL中


"No database selected" just in the second and following queries - PHP - MySQL

我有一个奇怪的错误,我只能执行mysql_query()一次。当我执行以下查询时,我得到No database selected

关键是,总是执行第一个查询(insertar()),但随后的查询失败(consultar()),表示没有选择数据库。我已经重写了代码,改为使用MyQSLi,但奇怪的是,第一个查询总是能再次工作,而下面的查询只是失败了,并出现了这个错误:Warning: mysqli::mysqli(): (HY000/1044): Access denied for user ''@'localhost' to database 'mysql'。我还尝试删除了insert,只执行了两个selects,你猜怎么着,只执行第一个select,下一个失败了。

注意:我知道我没有从consultar()返回任何内容,错误不存在,它是在到达该语句之前触发的。

这是我调用函数queries.php:的代码

insertar($nombre, $respuestas, $fallos, $tiempoTotal);
$puntuacionesTiempo = consultar("tiempo");
$puntuacionesFallos = consultar("fallos");

这是我的model.php:

<?php 
function conectar(){
    $data = include_once('configDB.php');
    $c = mysql_connect($data["server"], $data["user"], $data["pass"]);
    mysql_select_db("mysql", $c);
    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 consultar($filtro){
    $conexion = conectar();
    $consulta = "SELECT * FROM juegopreguntas ORDER BY tiempo LIMIT 5";
    $re = mysql_query($consulta, $conexion);
    if(!$re)
        echo "Hubo un fallo al consultar -> ".mysql_error();
    cerrarConexion($conexion);
}
function cerrarConexion($conexion){
    mysql_close($conexion);
}
?>
<?php 
    $data = include_once('configDB.php');
    $conexion  = mysql_connect($data["server"], $data["user"], $data["pass"]);
    mysql_select_db("mysql", $c);
    if ($c)
        return $c;
    else
        exit("fail");

function insertar($nombre, $resultados, $tiempo){
    global $conexion;
    $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 consultar($filtro){
      global $conexion;
    $consulta = "SELECT * FROM juegopreguntas ORDER BY tiempo LIMIT 5";
    $re = mysql_query($consulta, $conexion);
    if(!$re)
        echo "Hubo un fallo al consultar -> ".mysql_error();
    cerrarConexion($conexion);
}
function cerrarConexion($conexion){
    mysql_close($conexion);
}
?>