class Mysqli_Database.php does not return results in a prepared query

0

I am running the following code to make a query:

   public static function myfuncion(){ 
          $conm = new Database();
          $sql = "select 'user_nombre' from '".self::$tablename."' where 'user_nick' = ?;";
          $conm->prepare($sql);
          $conm->execute("Administrador");
          $query = $conm->results('array');
          return $query;
   }

being Database() the class found in Mysqli_Database and doing a var_dump(myclasse::myfuncion()); does not return a result, but if I eliminate the ...->results('array'); line then if I return the result, I do not know why this happens. Either way it is not the expected result.

    
asked by Joel 01.04.2018 в 20:48
source

1 answer

2

Since you are using a Github library, the documentation of it explains that prepared queries should be sent like this:

$results = $Mysqli_Database
    ->prepare("SELECT 'foo' FROM 'bar' WHERE 'foo' = ? OR 'foo' = ?;")
    ->execute("Timmy O'Toole", 2)
    ->results('array'); 

Try to do it as they indicate, chaining methods:

      $conm = new Database();
      $sql = "select 'user_nombre' from '".self::$tablename."' where 'user_nick' = ?;";
      $results = $conm
        ->prepare($sql)
        ->execute("Administrador");
        ->results('array');
      return $results;

If you were using your own connection MySQLi , the code would work like this:

You would be missing several things:

  • use bind_param to pass the query filter
  • store the result in some variable, using bind_result
  • return the value of that variable

The function should look like this:

   public static function myfuncion(){ 
          $conm = new mysqli("localhost", "usuario", "contraseña", "basedatos");
          $sql = "select 'user_nombre' from '".self::$tablename."' where 'user_nick' = ?;";
          $user_nick="Administrador";
          $stmt=$conm->prepare($sql);
          $stmt->bind_param("s",$user_nick);
          $stmt->execute();
          $stmt->bind_result($user_nombre);
          return $user_nombre;
   }

It's not bad to understand how PHP works inside:)

    
answered by 01.04.2018 в 21:29