0

So i'm having trouble using mysql_fetch_array. this is my code:

$sql=mysql_query("SELECT * FROM members WHERE group='$group'");
$query=mysql_fetch_array($sql);

i am getting the following error:

Warning: mysql_fetch_array() expects parameter 1 to be resource

Any help would be very appricated

7
  • 3
    since it's obvious that you're just learning PHP, let me save you a lot of time and tell you now that the mysql_xxx() functions are deprecated. If you're learning them now, you are learning obsolete techniques. You should consider learning up-to-date PHP techniques instead; eg the PDO library. Here's a tutorial to get you started: phpmaster.com/avoid-the-original-mysql-extension-2 Commented Jul 29, 2013 at 14:44
  • 1
    When developing code, include error reporting like PHP's (now deprecated) mysql_error() function. It makes things a whole lot easier to debug. Commented Jul 29, 2013 at 14:53
  • @Strawberry Thanks. i'll use php's mysql_error() function from now on ;) I forgot it existed, lol Commented Jul 29, 2013 at 14:55
  • If you're the one who created this schema, avoid creating columns or tables that are reserved keywords like GROUP. It's really annoying to have to deal with these sorts of names as special cases all the time. Commented Jul 29, 2013 at 15:07
  • You must be especially careful to properly escape all values when composing queries to avoid severe SQL injection bugs. The $group value here is not obviously escaped and could be a huge liability. If you use PDO and are disciplined about using parameterized queries you won't have these sorts of issues. Commented Jul 29, 2013 at 15:09

1 Answer 1

3

group is a mysql function, you must enclose it with ` so mysql know that it's a name and not the function. like this:

$sql=mysql_query("SELECT * FROM members WHERE `group`='$group'");
$query=mysql_fetch_array($sql);
Sign up to request clarification or add additional context in comments.

5 Comments

Wow, that just did it. Thank you very much! Been working around with this in like 2 days. Thank you very much
Why i couldn't see any difference between this answer and question? Is this question modified?
@Eray Yea, i just copy-pasted his code, and forgot to modify it. thanks for the comment
@Eray - the field name group has had backticks added to it. It is a MySQL keyword, so it was causing the query to fail, and thus $sql was false instead of a recordset, so the fetch function blew up. Some basic error checking would have been a good idea to help spot this kind of thing.
@Spudley thanks for assist. ahmad albayati got my problem. he forgot to modify it. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.