-2

The problem I'm having is that my Query:

SELECT * FROM administradores WHERE nombre_administrador=? AND password=?

isn't pulling anything out of the database.

var_dump shows:

  public 'affected_rows' => null, public 'insert_id' => null
  public 'num_rows' => null
  public 'param_count' => null
  public 'field_count' => null
  public 'errno' => null
  public 'error' => null
  public 'error_list' => null
  public 'sqlstate' => null
  public 'id' => null

login.php

<?php
    class login{
        //Variable for MySql connection
        private $hookup;
        private $sql;
        private $tableMaster;

        //Field Variables
        private $pass;
        private $name;

        public function __construct()
        {
            //Get table name and make connection
            $this->mysqli=UniversalConnect::doConnect();

            //Get data from HTML form
            $this->name=$_POST['name'];
            $this->pass=$_POST['pass'];
            //Call private methods for MySql operations
            $this->dologin();
            $this->mysqli->close();
        }

        private function dologin()
        {
            if ($stmt = $this->mysqli->prepare("SELECT * FROM administradores WHERE nombre_administrador=? AND password=?")){
                var_dump($stmt);
                /* bind parameters for markers */
                $stmt->bind_param('ss', $this->name, $this->pass);
                var_dump($this->name);
                var_dump($this->pass);
                /* execute query */
                $stmt->execute();

                /* bind result variables */
                $stmt->bind_result($id,$nombre_administrador,$password,$ubicacion,$nombre,$apellido);

                /* fetch value */
                $stmt->fetch();
                /*get number of rows*/
                $num_row =$stmt->num_rows;

                if($num_row==1){
                    echo 'true';
                    $_SESSION['Name'] = $row['nombre_administrador'];
                    $_SESSION['Id'] = $row['id'];
                    header("location:  http://localhost/SCAF1.0/indexSCAF.html");
                } else {
                    echo "<script>alert('wrong password');</script>";
                    echo 'false';
                }
            } else{
                echo "Falló la conexión con MySQL: (" . $this->mysqli->connect_errno . ") " . $this->mysqli->connect_error;
            }

            $stmt->close();
        }
    }
?>
GitaarLAB
  • 14,536
  • 11
  • 60
  • 80

1 Answers1

0

The backticks shouldn't be a problem but they aren't necessarily needed. It sounds like there might be a connection error.

slapyo
  • 2,979
  • 1
  • 15
  • 24
  • I removed the backticks, and still nothing, it doesn't display any query errors either – julio cesar villalba Oct 10 '14 at 22:24
  • Have you checked connection errors? `$this->mysqli->connect_errno` and `$this->mysqli->connect_error`. I would place those right after `$this->mysqli=UniversalConnect::doConnect();`. – slapyo Oct 10 '14 at 22:27
  • it doesn't show anyeither, actually it was working before, i changed the query and added the prepare portion, it was a plain substitucion in the query before, so im assuming i messed the prepare statement, but i checked the php manual and i can't figure out why wouldn't it work – julio cesar villalba Oct 10 '14 at 22:29
  • and it isn't that ,since it was working when it was just the query with variables, before I changed the code and added prepare – julio cesar villalba Oct 10 '14 at 22:35
  • What does is output of `var_dump($stmt);` right after you prepare the query? – slapyo Oct 10 '14 at 22:44
  • right after if ($stmt = $this->mysqli->prepare("SELECT * FROM administradores WHERE nombre_administrador=? AND password=?")){ it's all null – julio cesar villalba Oct 10 '14 at 22:45
  • Instead of using `var_dump($stmt);` just inside the IF statement, try this `echo "
    " . print_r($stmt, true). "
    ";`. What does that output?
    – slapyo Oct 10 '14 at 23:00
  • mysqli_stmt Object ( [affected_rows] => 0 [insert_id] => 0 [num_rows] => 0 [param_count] => 2 [field_count] => 6 [errno] => 0 [error] => [error_list] => Array ( ) [sqlstate] => 00000 [id] => 1 ) – julio cesar villalba Oct 10 '14 at 23:05
  • Looks like there is something when you do `var_dump($stmt)` that everything is `null` but if you do `print_r($stmt, true);` it actually shows what's in the object. Put that in after `$stmt->bind_param();`, and `$stmt->execute();`. What does it show in those places. – slapyo Oct 10 '14 at 23:08
  • it doesn't print anything – julio cesar villalba Oct 10 '14 at 23:11
  • It doesn't print anything after which one? Do you have error reporting turned on? `error_reporting(E_ALL);` – slapyo Oct 10 '14 at 23:11
  • after any, i placed both lines but it just doesnt do anything, and i just added error reporting, and still nothing – julio cesar villalba Oct 10 '14 at 23:14
  • So before `$stmt->bind_param();` it gave an output. But if you place it after, nothing gets displayed. Try `ini_set('display_errors',1);error_reporting(-1);` to turn on error reporting. What if you place a simple `echo 'Hello';` after `$stmt->bind_param();`? – slapyo Oct 10 '14 at 23:17
  • I'm not sure what to tell you. I would step through it line by line and watch what `$stmt` holds. Make sure the value's you're passing for `$this->name` and `$this->pass` actually contain the values you're expecting. It looks alright, I can't step through your code on my local machine. – slapyo Oct 10 '14 at 23:26
  • i understand thank you, i'll try if not, i might just start over haha – julio cesar villalba Oct 10 '14 at 23:48