warning mysqli_select_db () expects parameter 1 to be mysqli

1

I am doing a school platform, where I need to connect my database with my web page. However, this warning appears when I run it:

  

Warning: mysqli_select_db () expects parameter 1 to be mysqli, string   given in C: \ xampp \ htdocs \ Sor Juana \ config.php on line 16

And this is the PHP code:

<?php

function conectar () {
    // Create connection
    $dbhost = "localhost";
    $dbuser = "root";
    $dbpass = "";
    $dbname = "bd_preparatoria";
    $conexion = mysqli_connect($dbhost, $dbuser, $dbpass);
    //if (!$conexion) {
        //echo 'Error al conectar a la base de datos';
    //}
    //else {
        //echo 'Conectado a la base de datos';
    //}
    mysqli_select_db($dbname,$conexion);
    return $conexion;
    mysqli_query("SET NAMES utf8");
    mysqli_query("SET CHARACTER_SET utf");
}

?>
    
asked by Diego 27.03.2017 в 22:06
source

3 answers

1

You have changed the parameters. Try this mysqli_select_db($conexion,$dbname);

    
answered by 27.03.2017 в 22:18
0

There was some confusion in the code.

a. You were using two variables for the same thing: the name of the database ( see PHP Manual ).

b. The way to set the charset was not correct ( see PHP Manual )

c. You were doing the return before setting the chartset. In a function the last thing is the return, if it returns something.

<?php
function conectar(){
// Create connection
    $dbhost = "localhost";
    $dbuser = "root";
    $dbpass = "";
    $dbname = "bd_preparatoria";

$bd = "el nombre de tu base"; is the same as $ dbname

    $conexion = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);

    /* cambiar el conjunto de caracteres a utf8 */
    if (!mysqli_set_charset($conexion, "utf8")) {
        printf("Error cargando el conjunto de caracteres utf8: %s\n", mysqli_error($conexion));
    } else {
        printf("Conjunto de caracteres actual: %s\n", mysqli_character_set_name($conexion));
    }
    //Por último retornas la conexión con el charset utf8 seteado. 
    return $conexion;
}
?>
    
answered by 28.03.2017 в 00:27
0

I had the same problem a while ago, and it happens that using mysqli_select_db shows this problem, but if you leave it the following way, it can work for you (at least it worked for me):

The issue is not to separate the variable BD .

<?php

function conectar () {
    // Create connection
    $dbhost = "localhost";
    $dbuser = "root";
    $dbpass = "";
    $dbname = "bd_preparatoria";
    $bd = "el nombre de tu base";
    $conexion = mysqli_connect($dbhost, $dbuser, $dbpass, $bd);
    //if (!$conexion) {
        //echo 'Error al conectar a la base de datos';
    //}
    //else {
        //echo 'Conectado a la base de datos';
    //}
    mysqli_select_db($dbname,$conexion); // esto se lo quitas
    return $conexion;
    mysqli_query("SET NAMES utf8");
    mysqli_query("SET CHARACTER_SET utf");
}

?>

Please comment if it was useful.

    
answered by 27.03.2017 в 22:12