0

I am trying to get all the students from my table SCstudents that are in grade 11.

$students = mysqli_query($con, "SELECT * FROM SCstudents WHERE Grade='11'");

for some reason the above code does not return anything. I know this because when I try to run a loop with

mysqli_fetch_array($students)

there aren't any iterations.

Could this be because 11 is an integer and it is in quotations like a string? There are about 3 students whose grade is 11 in the table SCstudents.

  • 1
    Try your queries directly if your scripts fail, either trough phpmyadmin or similar, or command line. What happens when you run "SELECT * FROM SCstudents WHERE Grade=11" ? – JimL Jul 16 '13 at 14:30
  • **what** error it arise? – Your Common Sense Jul 16 '13 at 14:31
  • @JimL, it gives me the same message with quotations and without quotations. –  Jul 16 '13 at 14:34
  • are you sure `Grade` is an integer field. If it's a char/varchar and contains whitespace, e.g. '11[space]`, then `'11[space]' == '11'` is going to always be false. As well, a result set with no results (e.g. zero rows) is **NOT** an error. It's a valid result that simply happens to be empty. – Marc B Jul 16 '13 at 14:35
  • @Your Common Sense it gives me this mysqli_fetch_array() expects parameter 1 to be mysqli_result, null –  Jul 16 '13 at 14:35
  • You have a typo in your field `SCstudents` or `Grade` – Sablefoste Jul 16 '13 at 14:35
  • @user2540820 Then you know that wasn't the problem :) If you select students with grade 11 in phpmyadmin you will actually get the query used. Try to see how it differs from what you have. – JimL Jul 16 '13 at 14:37
  • it looks like $students misspelled. Either way, here is a way to find out: http://stackoverflow.com/a/15447204/285587 – Your Common Sense Jul 16 '13 at 14:39
  • @Marc B Grade has to be an integer field? I'm sure it has no spaces and I have a page in my website where I can see all of the columns of the table SCstudents, so I am sure that, ideally some rows must be returned. –  Jul 16 '13 at 14:41
  • what does `$students = mysqli_query($con,"SELECT * FROM SCstudents WHERE Grade='11'") or die(mysqli_error($con));` give you? – Sean Jul 16 '13 at 14:43
  • doesn't HAVE to be, but if you're storing numbers in it, then might as well use an actual number field. You can check for whitespace with something like `select Grade, LENGTH(Grade) WHERE Grade LIKE '%11%'`. that'll find any grade that contains `11` somewhere, and tell you how long the string is. If you get any lengths that AREN'T `2`, then you've got a padding problem. – Marc B Jul 16 '13 at 14:45

3 Answers3

0

Could this be because 11 is an integer and it is in quotations like a string?

No.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
0

This specific problem has got nothing to do with the query.

From the manual on mysqli_query():

Returns FALSE on failure. For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query() will return a mysqli_result object. For other successful queries mysqli_query() will return TRUE.

So $students is either boolean or a mysqli_result object, never null.

In the comment below the question you state that null is given so it would seem that $students variable - where you are using it - is not the same as where you define it.

Sounds like a variable scope problem but other possibilities include overwriting the variable, etc.

jeroen
  • 91,079
  • 21
  • 114
  • 132
0

When i run into such problem i run the query manually. But you did that already.

To check if you do not have any whitespaces in the table execute this query and check if you get any results.

SELECT * FROM SCstudents WHERE Grade LIKE '%11%'

If you would have any whitespaces before or after the 11 this should still give you the results and you know you are on the right track.

Gerhard
  • 176
  • 1
  • 4