2

I've been coding with PHP and MySQL for about a year now and have gotten the hang of it pretty well; constructing really complicated queries with joins and calculated fields and all the other joys of MySQL hasn't been a problem for me in months.

BUT there's something syntactically screwy with the following chunk of code that I can't figure out - even though it's impossibly simple, and, even more infuriating, is closely related to other parts of the project that I'm working on (and which works flawlessly).

Here's the problem code I'm trying to run, followed by the bugchecking I've already done to try to isolate the problem.

If anyone has any suggestions, I'd be totally grateful because I'm beginning to lose my mind.

Problem:

I'm really losing my mind over this, so please don't laugh when you see the code:

$query="SELECT count(somefield) FROM db_name WHERE otherfield='".$myvariable."'";

My query finds no results when using a certain variable as part of a field search - even though I know that there are over 900 records in the database that should match.

Bugchecking:

  • Because I know the value of the variable I'm passing to the query, I've tried hardcoding it into the query and it works fine.
  • I've run the query in the MySQL console (again, of course, hardcoded instead of with the variable) and it works fine.
    • To my mind, these two facts eliminate the possibility that there's something syntactically incorrect with the PHP version of the query.
  • In order to eliminate all possible database connection issues and to make sure the problem isn't related to iterating through the results returned, instead of trying to get the actual results, I've altered my original query to return only the count of the results and have incorporated the standard or die(mysql_error()) statements during the connection sequence. The query is executing, but is finding 0 results, so that eliminates the possibility that it's a connection issue.
  • I've verified that the field I'm checking is the correct field for the information I'm looking for (like I said, it runs fine if I hardcode the variable into the query... which, of course, will not be an option in the finished code).
  • I've checked the type of the variable before trying to pass it into the query (figuring that maybe, because it's pulled from a returned xml earlier in the script, that maybe it was showing up as an array or something). It typed as string.
  • I've verified that the variable is formatted in the way that I expect it to be found in the database; strtoupper, etc.
  • I've tried using LIKE '%".$myvariable."'"; still no dice.

Anyone have any suggestions for what I can do to figure out what the hell is going wrong? Thanks so much!

6
  • 1
    can you post the whole code block for this particular event? I want to verify what is defining $myvariable and make sure there's nothing in that var that is causing a problem, and also that it's not being overwritten before the query is run. Commented Nov 18, 2011 at 1:33
  • 1
    tl:dr, but have you tried running the output of var_dump($query) in phpmyadmin? Commented Nov 18, 2011 at 1:34
  • also, you could just write $query="SELECT count(somefield) FROM db_name WHERE otherfield='$myvariable'"; Commented Nov 18, 2011 at 1:36
  • do a print_r($query) and post the resulting string here. Commented Nov 18, 2011 at 1:40
  • Thanks guys... but, as I was rethinking it all (which is basically all I've done for the last 48 hours, because it seems SO SIMPLE), I realized I forgot to use mysql_real_escape_string($myvariable) because I was assuming (like a frigging idiot) that, because the contents of $myvariable came from xml, that would somehow magically make it ok. Sorry, and thanks for the help just the same. Thanks vascowhite, though for the tips; valuable for further use! Commented Nov 18, 2011 at 1:41

5 Answers 5

1

It's not a PHP issue so quotes have nothing to do with it. The query has no error, so you'll need to debug it step by step.

I. SELECT count(*) FROM table_name

II. SELECT count(*) FROM table_name WHERE field='$myvariable'

Where you're dealing with PHP variables in query, echo the query and run it directly in database to omit PHP's side of error.

III. SELECT count(somefield) FROM table_name WHERE field='$myvariable'

1
  • Correct me if I'm wrong, but count(*), count(someField) and count(0) should be the same, right? Commented Nov 18, 2011 at 3:43
1

Is the $myVariable escaped? If not, escape it using

$escapedVariable=mysql_real_escape_string($myVariable);

and then run

$query="SELECT count(somefield) FROM db_name WHERE otherfield='$escapedVariable'";
2
  • Yes, of course, I thought of that almost immediately after I posted my question! And it was a very good guess, as well, so thanks for the attempt, but no... that wasn't it... I have to answer all the replies until I answer my own question (which I have done), but, sincerely, thanks for trying to help. The problem was that $myvariable was being generated from processed XML and was being left untrimmed of white space! I am an idiot! 50 hours and asking on here proves it! Thanks again! Commented Nov 18, 2011 at 14:36
  • Aye, we all do something like that sometime or the other!! :) Commented Nov 19, 2011 at 5:58
1

Thanks so much to everyone who tried to help, but I figured it out several hours after posting: The first problem was that I forgot to use mysql_real_escape_string($myvariable)... the reasons for how and why I forgot are manifold, but there it is.

So, after plugging that guy back in (which I had ASSUMED had been in this particular module of my code in the first place, but that's where "assuming" gets you, lol), I thought I had the whole thing licked. Three hours later, and still nothing. THEN I realized that it had to be related to XML that was being parsed into the $myvariable... so around and around we went with that one for a few more hours.

FINALLY, I realized that the real culprit was my eyes (which aren't so great).. what looked like a perfectly legit quoted string while reading a debug echo of the query before running turned out to have leading and trailing white space (which, of course, I instantly removed with $myvariable=trim($myvariable, " "), and, that, of course, solved the entire problem...:< ... Yes, I am an idiot, and I'm sorry, but, after working over this UTTERLY INFURIATINGLY stupid line of code for over 48 hours (I'm used to writing things like:

$query="UPDATE db_one.table_one SET item1='".(string)$result_array[$i][1]."', item2='".(string)$result_array[$i][2]."' WHERE thing3=".(string)$result_array[$i][19];

... and other assorted fun nonsense), I had to resort to asking (because I - no pun intended - couldn't "see" the problem... ugh)... SO... I am an idiot, and I'm sorry (but encouraged by the efforts of all of you nice people who tried to help) and am sorry for wasting everyone's time. I need to learn how to handle XML much *more*.

Sorry and thanks again!

0

I always wrote it like this

$query="SELECT count(somefield) FROM db_name WHERE otherfield='$myvariable' ";

Try removing the double quotes and dot at the variable name

3
  • 3
    functionally they're identical. this would not affect the query at all Commented Nov 18, 2011 at 1:37
  • This would make NO difference, because essentially, both ways are identical Commented Nov 18, 2011 at 3:40
  • Kai Qing is absolutely correct; they are both syntactically identical, but props for trying to help just the same. As I mentioned in each of my earlier comments, I figured it out; it was that I was using untrimmed XML to supply the value for $myvariable (hence all the confusion and frustration... what can I say? I am a moron!)... Thanks for trying to help, though. Commented Nov 18, 2011 at 14:39
0

You shouldn't need the parenthesis around the var. Plus look at other changes.

$query="SELECT count(*) FROM table_name WHERE field='$myvariable'";
2
  • Cheers, and thanks for the help. While those are awesome stylistic alterations (and I appreciate them), the problem was with the fact that I was using untrimmed XML to create the value for $myvariable. But thanks so much for the pointers! Commented Nov 18, 2011 at 14:37
  • Hehe, we all have those moments that waste hours of our lives :D. Just a heads up. ALWAYS test a query with no variables if you have any errors. So swap $myvariable with the actual variable and see if it works. If it doesn't then it's a sql issue. If it works, then the var isn't set correctly. Commented Nov 18, 2011 at 19:11

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.