How to call a Mysqli connection in different functions?

0

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

    
asked by luis_gomez 07.06.2018 в 16:44
source

1 answer

0

To be able to use a variable that comes from a document included or from another part of the document within a function when it is called, we use the declaration global

File db.php

<?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());
}
?>

File of the function:

<?php
include("db.php")


function retornar_tipo_usuario(){
       global $mysqli;
       $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;
      }
}
?>

That way you can use the connection statement in the functions you need.

Another way would be to create a function in your db.php file:

<?php
 function db_connect() {

 $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());
 }

 return $mysqli;
}
?>

And since the functions:

<?php
include("db.php")


function retornar_tipo_usuario(){
       $mysqli = db_connect();
       $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;
      }
}
?>
    
answered by 07.06.2018 в 17:14