A
A
Alexander Sharomet2015-05-10 12:24:03
PHP
Alexander Sharomet, 2015-05-10 12:24:03

Unable to retrieve data from php database, what could be the problem?

Hello. Since once I say I'm a bad programmer.
I had such a problem when I want to display data from the database in this way, then everything works:

$res=$this->db->mysqli->query('SELECT * FROM user) or die (mysql_error());
    $data = array();
    while(($row=$res->fetch_assoc())!=false){
      $data[]=$row;
    }
    echo $data; //Получаю массив

but if I need the data of a certain user
$email='[email protected]';
    $res=$this->db->mysqli->query('SELECT * FROM user WHERE email='.$email);
    $data = array();
    while(($row=$res->fetch_assoc())!=false){//ОШИБКА
      $data[]=$row;
    }
    echo $data;

then the error
Fatal error: Call to a member function fetch_assoc() on a non-object in file path is thrown.
How can this be corrected? Thanks!)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
reifter, 2015-05-10
@sharomet

email string, and strings are quoted

$res=$this->db->mysqli->query("SELECT * FROM user WHERE email='".mysql_real_escape_string($email)."'");

A
asd111, 2015-05-10
@asd111

You have SQL injection. Use PDO and prepared statements. Better yet, take yii2

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question