Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP/MYSQL: What's Wrong With My Query/ PHP variable in query
    primarykey
    data
    text
    <p>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.</p> <p>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).</p> <p>Here's the problem code I'm trying to run, followed by the bugchecking I've already done to try to isolate the problem.</p> <p>If anyone has any suggestions, I'd be totally grateful because I'm beginning to lose my mind.</p> <h1>Problem:</h1> <p>I'm really losing my mind over this, so please don't laugh when you see the code:</p> <pre><code>$query="SELECT count(somefield) FROM db_name WHERE otherfield='".$myvariable."'"; </code></pre> <p>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.</p> <h1>Bugchecking:</h1> <ul> <li>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. </li> <li>I've run the query in the MySQL console (again, of course, hardcoded instead of with the variable) and it works fine. <ul> <li>To my mind, these two facts eliminate the possibility that there's something syntactically incorrect with the PHP version of the query. </li> </ul></li> <li>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 <code>or die(mysql_error())</code> 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. </li> <li>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). </li> <li>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. </li> <li>I've verified that the variable is formatted in the way that I expect it to be found in the database; strtoupper, etc. </li> <li>I've tried using <code>LIKE '%".$myvariable."'";</code> still no dice.</li> </ul> <p>Anyone have any suggestions for what I can do to figure out what the hell is going wrong? Thanks so much!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload