I am migrating my web from mysql to mysqli, before I had a file called db.php which had this connection to the database: db.php
<?php
$dbhost = "localhost"; // El host
$dbuser = "root"; // El usuario
$dbpass = ""; // El Pass
$db = "web"; // Nombre de la base
$connect=mysql_connect("$dbhost","$dbuser","$dbpass"); // se conecta con la db
mysql_select_db("$db")or die(mysql_error());
?>
then I had a script with different functions called operations.php in which I did an include at the beginning to call the connection and then the functions and their respective codes:
<?php
include("db.php")
function retornar_tipo_usuario(){
$sql=mysql_query("SELECT
perfiles.perfiles_id as id_perfil
FROM
usuarios
INNER JOIN perfiles ON (usuarios.usuarios_id_perfil=perfiles.perfiles_id)");
while($row = mysql_fetch_array($sql)){
$id_tipo_usuario=$row["id_perfil"];
return $id_tipo_usuario;
}
}
?>
until then everything worked fine but now I am using mysqli and I do the same procedure but when executing the query it tells me that it is null, so it is not taking the connection to the database
<?php
$dbhost = "localhost"; // El host
$dbuser = "root"; // El usuario
$dbpass = ""; // El Pass
$db = "web"; // Nombre de la base
$mysqli = new mysqli($dbhost, $dbuser,$dbpass, $db);
if ($mysqli -> connect_errno) {
die( "Fallo la conexión a MySQL: (" . $mysqli -> mysqli_connect_errno()
. ") " . $mysqli -> mysqli_connect_error());
}
?>
operaciones.php
<?php
include("db.php")
function retornar_tipo_usuario(){
$sql=("SELECT
perfiles.perfiles_id as id_perfil
FROM
usuarios
INNER JOIN perfiles ON (usuarios.usuarios_id_perfil=perfiles.perfiles_id)");
$sql = $mysqli->query($sql);
while($row = $sql->fetch_assoc()){
$id_tipo_usuario=$row["id_perfil"];
return $id_tipo_usuario;
}
}
?>
then I have several more functions but none connects me and throws me the following error: Fatal error: Call to a member function query() on null in