Error while mysql_fetch

hey folks,
i m getting a error. my whole code is working fine. i don’t know why this part isn’t working. i am getting this error
mysql_fetch_array() expects parameter 1 to be resource
and this is my code


$content = mysql_query("SELECT * FROM subjects where subject_id = {$subjects["menu_name"]}");
while($heading = mysql_fetch_array($content)){
    echo "$heading[1]";
}

where am i going wrong?

Nothing wrong with mysql_fetch_array its the SQL you’ve written.

You’ve got double quotes in your $subjects array.

You have a double quote at the beginning before select, and then one after [ which ends the query so mysql will not understand it. To confuse it further you then have menu_name in the middle doing nothing and then start another query with “]}”. If you used mysql_error() as recommended in every tutorial I’ve seen this would have shown you that there was a sql syntax error.

In effect you have two queries (both of which contain syntax errors):
1: “SELECT * FROM subjects where subject_id = {$subjects[”
2: “]}”

Your query should look like this:


//Note that the VALUE of a variable must be inside SINGLE quotes.
//You do not need any quote marks for an array that is inside a double quoted string
$content = mysql_query("SELECT * FROM subjects where subject_id = '$subjects[menu_name]'");

oic. though i get much confusing while using functions. like i can’t wrap my head around when u pass argument in function. basically this is a query. i wanna do in a function but i fail to head the argument passing and how it work

What you’re saying is irrelevant. You had badly formed SQL. Your main problem is understanding how to create a string successfully and how to use quotation marks.

He actually wasn’t far off, though… this would work:

$content = mysql_query("SELECT * FROM subjects WHERE subject_id = {$subjects[menu_name]}");  

No it wouldn’t. Inside the curly braces PHP would expect single quote marks and you’ve used none. Instead it would throw up an error saying its an undefined constant and its assumed the use of it. The use of braces inside double quoted strings is so that you can access arrays and multidimensional arrays by using the single quotes.

I’ll concede to that… I’ve never actually used arrays as such, but I know indexed arrays can be used in double-quoted strings with curly braces, and I’ve seen array tokens used with no quotes (as in my example above)… so I assumed that would work… but single quotes work for associative arrays in a double-quoted string, then? Means he was even closer. :slight_smile:

No. The only time you can use single quotes in an array inside double quotes is if the array variable is inside braces as I’ve mentioned.

He was using double quotes for the array variable inside braces which doesn’t work. Unless it is escaped, php will see the 2nd double quote as a string terminator. I’ve already explained this above so I won’t do it again.

So what you’re really trying to say is “Yes, single quoted assoc arrays will work inside double-quotes when (properly) encapsulated in braces.” :slight_smile:

Steve you sound like you’re trying to turn it around to make me agree with what you’re saying.

I’ve just explained it to YOU - who above openly admitted to not even using arrays.

You’ve not suggested that single quotes work with arrays inside braces. It can and does work without them but only with error reporting turned off to shut up the warnings and it isn’t technically correct.

I don’t need to agree with my own explanation. This isn’t a competition as to who is right or wrong its about being factual.

I’m not the one making it a competition - on the contrary, you’re the one being contentious and making assumptions about what I know… I’m only pointing out that your answer to my question was better phrased “YES (under certain conditions)” instead of “NO (EXCEPT under certain conditions)”. The former implies an intent to educate, the latter implies an intent to disprove or argue…

Cheers.

Steve,

Bottom line, you openly admit you don’t do arrays and you’ve jumped into the topic trying to post a fix which was wrong.

I’m saying no more to you in this topic. In other topics I welcome communication with you.

ok i m lost now in the conservation. what did i missed or was doing wrong?

See post 2. Thats all that is important.

No, you misinterpreted what I said to mean that. I actually know quite a lot about arrays… what I admitted is that I’ve specifically never used an associative array inside a double-quoted string before… because it doesn’t fit into my coding style (“as such” means “like that”). However, I was aware that it’s possible, and I posted what I believed to be the solution.

Last I checked, this was an open forum, and adding additional solutions to a problem was generally perceived as good practice… the fact that you answered first doesn’t make it “your thread”, and the fact that I was wrong doesn’t mean that I’m THE DEVIL…

Chill out a bit. This isn’t a “who’s smarter” challenge. Do you even see how aggressive your tone is?

Dude you’ve misunderstood me completely. Seriously, PM me if you feel that strongly.

You’ve picked apart that one line reading far too much into it yet you completely ignore the next line which says I welcome hearing from you. You’re telling me to chill?

Seriously, PM me.