Fatal error: Call to a member function fetch_assoc() on a non-object/ boolean

HI

Plz find the code
$query = "SELECT * FROM $this->_table_users WHERE manager_code = $manager_code AND date_leave = 0 ORDER BY emp_code DESC LIMIT $this->_offset,$this->_limit";
$query = mysqli_query($this->_dbconnect, $query);
$result = array();
$i = 0;
while($res = $query->fetch_assoc()){  //Error 
$result[$i] = $res;
$i++;
}
return $result;

Open in new window

it shows Fatal error suppose manager_code  =car1002, Xyz007, (alpha numeric value)
  if manager_code = 1001, 2003 (numeric)  works fine and don't get any error
prashanth agsoftware engineer Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
This error occurs because the query failed.  MySQL is not a black box - it can and will fail for reasons that are not always 100% in your control.  To find out why it failed, add these statements after line 2:

if (!$query)
{
    var_dump(mysqli_error($this->_dbconnect));
}

Open in new window

0
Julian HansenCommented:
it shows Fatal error suppose manager_code  =car1002, Xyz007, (alpha numeric value)
  if manager_code = 1001, 2003 (numeric)  works fine and don't get any error

In a sense you have answered your own question. When you send queries to a database, any fields not enclosed in quotes are assumed to be numeric, or to put it another way - if you send a non numeric field to the database not enclosed in quotes it will fail.

Therefore you need to do the following

$query = "SELECT * FROM {$this->_table_users} WHERE manager_code = '{$manager_code}' AND date_leave = 0 ORDER BY emp_code DESC LIMIT {$this->_offset},{$this->_limit}";

Open in new window


Note
a) The single quotes around the $manager_code value
b) the { } around the in-string variables

The { } around the variables are not necessary but they help PHP to determine what in the in-string variable is part of the variable name and what is not - I use them by default.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ray PaseurCommented:
What is the output of var_dump()?
0
Ray PaseurCommented:
@julianH: Sorry, but I do not have enough time left in life to guess about what novice programmers are doing.  That's why we ask pointed questions -- we want to lead them to the water, even if we cannot make them drink.  I don't really care about the exact solution so much as teaching the process of finding the solution.  I already know the solutions.  I just want someone who asks a question here to leave with a new tool for finding the next solution!  Sometimes that is a process of learning what question to ask of themselves, like, "What is in my variable?"
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.