sqlnoob Posted November 30, 2011 Share Posted November 30, 2011 $telresult = mysql_query("SELECT COUNT(`word`) FROM `hallofryslan_rhymewords` WHERE `word ='".$rymwurd."'"); $telrow = mysql_fetch_array($telresult); $telwurd = $telrow['COUNT(word)']; echo "<P>Aanwezig: ".$telwurd."</P>";` hi I've been trying for ages to find the proper syntax. Somehow this doesn't work. It counts 0 when I know the word exists in the table. Just how do I select a php variable? The variable is a word! Quote Link to comment https://forums.phpfreaks.com/topic/252127-mysql-proper-syntax-for-where-on-php-variable/ Share on other sites More sharing options...
Pikachu2000 Posted November 30, 2011 Share Posted November 30, 2011 Ypu're missing a closing ` backtick after `word, and you have no logic in place to check for errors. Quote Link to comment https://forums.phpfreaks.com/topic/252127-mysql-proper-syntax-for-where-on-php-variable/#findComment-1292621 Share on other sites More sharing options...
sqlnoob Posted November 30, 2011 Author Share Posted November 30, 2011 Ypu're missing a closing ` backtick after `word, and you have no in place logic to check for errors. still doesn't work with closing ` Quote Link to comment https://forums.phpfreaks.com/topic/252127-mysql-proper-syntax-for-where-on-php-variable/#findComment-1292623 Share on other sites More sharing options...
sqlnoob Posted November 30, 2011 Author Share Posted November 30, 2011 gah i just want to fetch the damn row and it won't work Quote Link to comment https://forums.phpfreaks.com/topic/252127-mysql-proper-syntax-for-where-on-php-variable/#findComment-1292625 Share on other sites More sharing options...
sqlnoob Posted November 30, 2011 Author Share Posted November 30, 2011 $telresult = mysql_query("SELECT COUNT(`word`) FROM `hallofryslan_rhymewords` WHERE `word` ='".$rymwurd."'"); $telrow = mysql_fetch_array($telresult); $telwurd = $telrow['COUNT(word)']; still doesn't work Quote Link to comment https://forums.phpfreaks.com/topic/252127-mysql-proper-syntax-for-where-on-php-variable/#findComment-1292627 Share on other sites More sharing options...
Pikachu2000 Posted November 30, 2011 Share Posted November 30, 2011 You have the query string embedded in the query execution function, which prevents you from echoing it for debugging, and you aren't checking to see if the query even succeeds. $query = "SELECT field FROM table"; if( $result = mysql_query($query) ) { // query succeeded, process results } else { // query failed echo "<br>Query $query<br>Produced error: " . mysql_error() . '<br>'; } Quote Link to comment https://forums.phpfreaks.com/topic/252127-mysql-proper-syntax-for-where-on-php-variable/#findComment-1292629 Share on other sites More sharing options...
sqlnoob Posted November 30, 2011 Author Share Posted November 30, 2011 $telresult = mysql_query("SELECT COUNT(`word`) FROM `hallofryslan_rhymewords` WHERE `word` ='".$rymwurd."'"); $telrow = mysql_fetch_array($telresult); $telwurd = $telrow[COUNT(word)]; another failed attempt HOW? Quote Link to comment https://forums.phpfreaks.com/topic/252127-mysql-proper-syntax-for-where-on-php-variable/#findComment-1292630 Share on other sites More sharing options...
sqlnoob Posted November 30, 2011 Author Share Posted November 30, 2011 $query = ("SELECT COUNT(`word`) FROM `hallofryslan_rhymewords` WHERE `word` ='".$rymwurd."'"); if( $telresult = mysql_query($query) ) { $telrow = mysql_fetch_array($telresult); $telwurd = $telrow[COUNT(word)]; echo "<P>Aanwezig: ".$telwurd."</P>"; } else { echo "<br>Query $query<br>Produced error: " . mysql_error() . '<br>';} nope still doesn't work sigh Quote Link to comment https://forums.phpfreaks.com/topic/252127-mysql-proper-syntax-for-where-on-php-variable/#findComment-1292634 Share on other sites More sharing options...
sqlnoob Posted November 30, 2011 Author Share Posted November 30, 2011 $query = "SELECT COUNT(`word`) FROM `hallofryslan_rhymewords` WHERE `word` ='".$rymwurd."'"; if( $telresult = mysql_query($query) ) { $telrow = mysql_fetch_array($telresult); $telwurd = $telrow[COUNT(word)]; echo "<P>Aanwezig: ".$telwurd."</P>"; } else { echo "<br>Query $query<br>Produced error: " . mysql_error() . '<br>';} again doesn't work Quote Link to comment https://forums.phpfreaks.com/topic/252127-mysql-proper-syntax-for-where-on-php-variable/#findComment-1292637 Share on other sites More sharing options...
Pikachu2000 Posted November 30, 2011 Share Posted November 30, 2011 Echo the query string, paste it into phpMyAdmin, or whatever you use, and see if it returns the expected results. Beyond that, you probably aren't going to get much help until you can provide a better description of the problem than "it doesn't work". Quote Link to comment https://forums.phpfreaks.com/topic/252127-mysql-proper-syntax-for-where-on-php-variable/#findComment-1292644 Share on other sites More sharing options...
sqlnoob Posted November 30, 2011 Author Share Posted November 30, 2011 echoing won't help either OK here is a description of the problem: I have a table with 416000 words! The user posts a word. This word is what I want to count. (I want to check if it exists in the table) I test it with Adam (I know this word is in the table), yet the result is 0, but it should be 1. Quote Link to comment https://forums.phpfreaks.com/topic/252127-mysql-proper-syntax-for-where-on-php-variable/#findComment-1292651 Share on other sites More sharing options...
sqlnoob Posted November 30, 2011 Author Share Posted November 30, 2011 in phpmyadmin: SELECT * FROM `hallofryslan_rhymewords` WHERE word ="Adam" empty result (would be 0 with count) SELECT * FROM `hallofryslan_rhymewords` WHERE word = Adam error 1054 Unknown column 'Adam' in 'where clause' SELECT * FROM `hallofryslan_rhymewords` WHERE word = 'Adam' empty result (would be 0 with count) SELECT * FROM `hallofryslan_rhymewords` WHERE id =31 result is id word 31 Adam (this list one is to show that the row does exist) Quote Link to comment https://forums.phpfreaks.com/topic/252127-mysql-proper-syntax-for-where-on-php-variable/#findComment-1292654 Share on other sites More sharing options...
mikosiko Posted November 30, 2011 Share Posted November 30, 2011 in phpmyadmin: .... SELECT * FROM `hallofryslan_rhymewords` WHERE word = 'Adam' SELECT * FROM `hallofryslan_rhymewords` WHERE id =31 result is id word 31 Adam (this list one is to show that the row does exist) if those 2 results are different then obviously your field `word` contains something different to 'Adam'... most likely a leading space or hidden character. You can try to trim blank spaces or use LIKE ... as in WHERE word LIKE '%Adam%' adding to what Picachu already told you... you are no controlling errors .. and yes echo helps to debug your code.... add this lines at the beginning of your php file (after <?php) // Define how to display/report errors ini_set("display_errors", "1"); error_reporting(E_ALL); it should reveal that you have also an error in your code.... specifically in this line $telwurd = $telrow[COUNT(word)]; Quote Link to comment https://forums.phpfreaks.com/topic/252127-mysql-proper-syntax-for-where-on-php-variable/#findComment-1292666 Share on other sites More sharing options...
sqlnoob Posted November 30, 2011 Author Share Posted November 30, 2011 that still will get me nowhere ^ remember the fetch has to be done on a php variable, not a mere LIKE alone I need the proper syntax for this: SELECT COUNT(word) FROM `hallofryslan_rhymewords` WHERE word ='$externalvariable' but anyhow thanks for making it apparent to me that the words in the tablecolumn word may be corrupted with spaces that may possible be at least 1 step in the good direction to solving this highly frustrating problem (damn just what I needed, spaces behind the words, this is may turn out to be one very crappy database (shouts in frustration, what more surprises will I get next, /me is most displeased)) Quote Link to comment https://forums.phpfreaks.com/topic/252127-mysql-proper-syntax-for-where-on-php-variable/#findComment-1292696 Share on other sites More sharing options...
mikosiko Posted November 30, 2011 Share Posted November 30, 2011 that still will get me nowhere ^ remember the fetch has to be done on a php variable, not a mere LIKE alone ... then you are not reading nor thinking in the solutions/suggestions offered to you, that indeed will solve your problem if you take the time to test and adjust them... trim or use LIKE with a variable is 101 coding. We can't see your data, but we already told you what you have wrong in your code (and I offer also a guessing for your wrong data)... the rest is on you. Quote Link to comment https://forums.phpfreaks.com/topic/252127-mysql-proper-syntax-for-where-on-php-variable/#findComment-1292729 Share on other sites More sharing options...
sqlnoob Posted November 30, 2011 Author Share Posted November 30, 2011 I appreciate the fact that you're trying to help, but I really need to know what the proper way is to write the WHERE clause and echo the result. For instance, why is this wrong: $telresult = mysql_query("SELECT * FROM `hallofryslan_rhymewords` WHERE `word` ='$someword'"); $telrow = mysql_fetch_array($telresult); $telwurd = $telrow['word']; And how do I write that the correct way? Also what I meant with "that will get me nowhere" is because the LIKE causes difficulties in the later part of the script (it's really complicated bit that searches for words that rhyme) and remember there is like 416000 of those words (now I know that's a Gordian knot and I have solution for that). But I still need to know what the proper way is. Just one good example with a php variable would do. Quote Link to comment https://forums.phpfreaks.com/topic/252127-mysql-proper-syntax-for-where-on-php-variable/#findComment-1292732 Share on other sites More sharing options...
fenway Posted November 30, 2011 Share Posted November 30, 2011 You've been told to use LIKE instead of =. REGEXP might work, too. Quote Link to comment https://forums.phpfreaks.com/topic/252127-mysql-proper-syntax-for-where-on-php-variable/#findComment-1292751 Share on other sites More sharing options...
mikosiko Posted November 30, 2011 Share Posted November 30, 2011 I appreciate the fact that you're trying to help, but I really need to know what the proper way is to write the WHERE clause and echo the result. For instance, why is this wrong: $telresult = mysql_query("SELECT * FROM `hallofryslan_rhymewords` WHERE `word` ='$someword'"); $telrow = mysql_fetch_array($telresult); $telwurd = $telrow['word']; And how do I write that the correct way? is nothing syntactically wrong with that sentence... is incomplete (no error control) but no wrong, could cause problems depending on your objectives.. yes... but that is a different topic. the first thing you have to do is fix your data inconsistency (or adjust your select to deal with it) and secondly stick with one version of your code.. otherwise is difficult for anybody try to help you. look for the suggestions that has been offered by Pica and other posts and you will be fine ... fenway suggestion to use REGEXP instead of LIKE is good too. this code is the best one that you can continue working on : $query = ("SELECT COUNT(`word`) FROM `hallofryslan_rhymewords` WHERE `word` ='".$rymwurd."'"); if( $telresult = mysql_query($query) ) { $telrow = mysql_fetch_array($telresult); $telwurd = $telrow[COUNT(word)]; // YOU MUST FIX THIS LINE... COUNT(word) <> COUNT(`word`)... you can also use an alias instead echo "<P>Aanwezig: ".$telwurd."</P>"; } else { echo "<br>Query $query<br>Produced error: " . mysql_error() . '<br>'; } Quote Link to comment https://forums.phpfreaks.com/topic/252127-mysql-proper-syntax-for-where-on-php-variable/#findComment-1292762 Share on other sites More sharing options...
fenway Posted November 30, 2011 Share Posted November 30, 2011 Using an alias would make that pesky COUNT() problem go away. Quote Link to comment https://forums.phpfreaks.com/topic/252127-mysql-proper-syntax-for-where-on-php-variable/#findComment-1292788 Share on other sites More sharing options...
sqlnoob Posted November 30, 2011 Author Share Posted November 30, 2011 is nothing syntactically wrong with that sentence... is incomplete (no error control) but no wrong, could cause problems depending on your objectives.. yes... but that is a different topic. thanks! That's what I wanted to know. So the only other obvious answer is that the database table must be corrupted Quote Link to comment https://forums.phpfreaks.com/topic/252127-mysql-proper-syntax-for-where-on-php-variable/#findComment-1292810 Share on other sites More sharing options...
sqlnoob Posted November 30, 2011 Author Share Posted November 30, 2011 Using an alias would make that pesky COUNT() problem go away. yeah it doesn't fetch an alias either, gives as much problems as no alias (I know it sounds silly) Just for clarity sake I initially wrote it with alias, when that didn't work I tried without it. [rant] I'm beginning to think there is something seriously wrong or weird with this webhost. Just out of curiosity, is it at all common to see the sql file when you upload with ftp? By the way never had this much difficulty obtaining the password and url to the phpadmin and I've got experience with like 5 other hosts, none of which are as tedious or difficult as this one. [/rant] Quote Link to comment https://forums.phpfreaks.com/topic/252127-mysql-proper-syntax-for-where-on-php-variable/#findComment-1292812 Share on other sites More sharing options...
sqlnoob Posted November 30, 2011 Author Share Posted November 30, 2011 You've been told to use LIKE instead of =. REGEXP might work, too. might try that one too, see if it works and if it doesn't work I'm going to recommend another webhost instead of this crappy and frustrating **** Quote Link to comment https://forums.phpfreaks.com/topic/252127-mysql-proper-syntax-for-where-on-php-variable/#findComment-1292813 Share on other sites More sharing options...
Pikachu2000 Posted December 1, 2011 Share Posted December 1, 2011 Instead of changing hosts, you should start listening to the people that are trying to help you instead of just responding with "that won't work" when you obviously haven't tried anything that was suggested. Since you seem to already know everything that won't work, it should be no problem for you to deduce what will work and fix it yourself. Quote Link to comment https://forums.phpfreaks.com/topic/252127-mysql-proper-syntax-for-where-on-php-variable/#findComment-1292886 Share on other sites More sharing options...
fenway Posted December 1, 2011 Share Posted December 1, 2011 I highly doubt that the webhost is to blame -- or that the database is corrupt. Quote Link to comment https://forums.phpfreaks.com/topic/252127-mysql-proper-syntax-for-where-on-php-variable/#findComment-1293096 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.