emopoops Posted December 10, 2009 Share Posted December 10, 2009 caps because its absolutely rediculous. i dontunderstand why its not working. there is a column called id and a collumn called to then there is also. two rows with the field for `to` equal to the number: 1 still doesnt work: just outputs . this user has no commnets, none. (every single time) :'( <?php $testlll = mysql_query("SELECT to FROM cmnts WHERE to = 1"); if(!$testlll){echo "this user has no comments, none.";} else{echo "this user has $testlll comments";} echo $testlll; ?> and of course $testlll (which i renamed trying to fix it -from $test) is equal to, well. NOTHING!!! whats going on here? do i have to change a table name or something? i didnt try that because i would have to redo the whole table. theres only one column called to and one column called id (so no dupes) also im not dumb it used to be $testlll = mysql_num_rows(mysql_query thing but that didnt work either. Quote Link to comment https://forums.phpfreaks.com/topic/184597-whyisnt-this-mysql-statemnt-not-working-php/ Share on other sites More sharing options...
ngreenwood6 Posted December 10, 2009 Share Posted December 10, 2009 in mysql to is a reserved keyword http://dev.mysql.com/doc/refman/5.0/en/reserved-words.html Quote Link to comment https://forums.phpfreaks.com/topic/184597-whyisnt-this-mysql-statemnt-not-working-php/#findComment-974494 Share on other sites More sharing options...
emopoops Posted December 10, 2009 Author Share Posted December 10, 2009 thank you. i will try changing it. it mos tlilely will work lol! Quote Link to comment https://forums.phpfreaks.com/topic/184597-whyisnt-this-mysql-statemnt-not-working-php/#findComment-974496 Share on other sites More sharing options...
premiso Posted December 10, 2009 Share Posted December 10, 2009 You could have an error, "to" column is a reservered word mysql_query("SELECT to FROM cmnts WHERE to = 1") or trigger_error("Query Failed: " . mysql_error()); Using that will show you what to error is. To fix it either change the column name (I recommend this) or use backticks to surround the column name: mysql_query("SELECT `to` FROM cmnts WHERE `to` = 1") or trigger_error("Query Failed: " . mysql_error()); I would highly recommend getting used to doing or trigger_error as I have shown above as it will save you headache by showing you what the errors are. Quote Link to comment https://forums.phpfreaks.com/topic/184597-whyisnt-this-mysql-statemnt-not-working-php/#findComment-974497 Share on other sites More sharing options...
CL Posted December 10, 2009 Share Posted December 10, 2009 Also you might want to try something like: $testlll = mysql_query("SELECT COUNT(*) AS to FROM cmnts"); $testarray = mysql_fetch_row($testlll); if($testarray[0] == 0) { echo "This user has no comments, none."; } else { echo "This user has " . $testarray[0] . " comments."; } Quote Link to comment https://forums.phpfreaks.com/topic/184597-whyisnt-this-mysql-statemnt-not-working-php/#findComment-974499 Share on other sites More sharing options...
emopoops Posted December 10, 2009 Author Share Posted December 10, 2009 this count() thing is this use less system resorces? ive never used it befroe. Quote Link to comment https://forums.phpfreaks.com/topic/184597-whyisnt-this-mysql-statemnt-not-working-php/#findComment-974501 Share on other sites More sharing options...
premiso Posted December 10, 2009 Share Posted December 10, 2009 If you use Count(), do not use Count(*) use Count(column) as it will be more efficient since it is only counting one column but does essentially the same thing. Doing that will be more efficient, as for your scenario it should be better doing that as CL pointed out. Quote Link to comment https://forums.phpfreaks.com/topic/184597-whyisnt-this-mysql-statemnt-not-working-php/#findComment-974536 Share on other sites More sharing options...
emopoops Posted December 10, 2009 Author Share Posted December 10, 2009 oh ok well i useally just do the mysql_query thing with mysql_num_rows for the count . why is this other way better? i dont understand Quote Link to comment https://forums.phpfreaks.com/topic/184597-whyisnt-this-mysql-statemnt-not-working-php/#findComment-974580 Share on other sites More sharing options...
trq Posted December 10, 2009 Share Posted December 10, 2009 WHYISNT THIS MYSQL STATEMNT NOT WORKING? Does that mean it is working then? Quote Link to comment https://forums.phpfreaks.com/topic/184597-whyisnt-this-mysql-statemnt-not-working-php/#findComment-974589 Share on other sites More sharing options...
premiso Posted December 10, 2009 Share Posted December 10, 2009 why is this other way better? i dont understand It all depends on what you want out. If all you want is the number of rows, it is better to just pull the COUNT(column) because it does not return any data just the number of rows. If you want to actually use the data then you would want to pull out the data and do the num_rows. This will make your script more efficient as MySQL does not have to pull out any data. Does that mean it is working then? You have to love double negatives. Quote Link to comment https://forums.phpfreaks.com/topic/184597-whyisnt-this-mysql-statemnt-not-working-php/#findComment-974782 Share on other sites More sharing options...
emopoops Posted December 12, 2009 Author Share Posted December 12, 2009 lol oops. i stil dont get how to use count. i guess im just acustomed to pulling out the lowest field that has like a varchar of 1 cause count makes no sense to me Quote Link to comment https://forums.phpfreaks.com/topic/184597-whyisnt-this-mysql-statemnt-not-working-php/#findComment-975858 Share on other sites More sharing options...
ngreenwood6 Posted December 12, 2009 Share Posted December 12, 2009 count isnt used to get values from the database. essentially it does the same thing as mysql_num_rows. If you were to do this: "SELECT name, count(age) as count_age FROM people" you could then access the count in your code by doing: echo $row['count_age']; This would give you the count of the rows with age, but instead of counting all the results you are just counting the age row. Quote Link to comment https://forums.phpfreaks.com/topic/184597-whyisnt-this-mysql-statemnt-not-working-php/#findComment-975864 Share on other sites More sharing options...
emopoops Posted December 12, 2009 Author Share Posted December 12, 2009 i think i was getting confused on how to output the count. cause usually i use something like $row = mysql_fetch_array($var) for getting data. but this $row[field] is the row part buildt in or somethin i dont understand where its coming from Quote Link to comment https://forums.phpfreaks.com/topic/184597-whyisnt-this-mysql-statemnt-not-working-php/#findComment-975870 Share on other sites More sharing options...
ngreenwood6 Posted December 12, 2009 Share Posted December 12, 2009 no your right when you are in your while loop you can access the variable like you would a field like this: while($row = mysql_fetch_array($query)){ echo $row['count_age']; } I had just omitted it out of my example Quote Link to comment https://forums.phpfreaks.com/topic/184597-whyisnt-this-mysql-statemnt-not-working-php/#findComment-975873 Share on other sites More sharing options...
emopoops Posted December 12, 2009 Author Share Posted December 12, 2009 ok so count_age is a made up thing. that is how u get the amount from the count? Quote Link to comment https://forums.phpfreaks.com/topic/184597-whyisnt-this-mysql-statemnt-not-working-php/#findComment-975881 Share on other sites More sharing options...
ngreenwood6 Posted December 12, 2009 Share Posted December 12, 2009 no this is a count. so if you have 5 entries in the database that match the criteria that you queried it will show 5 as the count. it doesnt add up those 5 values in the fields just tells you how many rows were returned. Quote Link to comment https://forums.phpfreaks.com/topic/184597-whyisnt-this-mysql-statemnt-not-working-php/#findComment-975886 Share on other sites More sharing options...
emopoops Posted December 12, 2009 Author Share Posted December 12, 2009 no i meant the word. u were all SELECT count(field) as the_word like dou just make a word up and use it after "as" ? Quote Link to comment https://forums.phpfreaks.com/topic/184597-whyisnt-this-mysql-statemnt-not-working-php/#findComment-975895 Share on other sites More sharing options...
cags Posted December 12, 2009 Share Posted December 12, 2009 AS is a keyword that creates an Alias for a field. It is simply the name that item will then have in the dataset returned. The AS keyword is generally optional and SELECT COUNT(field) the_word can normally be used, but since your new I'd recommend using it as it helps keep things clear. Quote Link to comment https://forums.phpfreaks.com/topic/184597-whyisnt-this-mysql-statemnt-not-working-php/#findComment-975918 Share on other sites More sharing options...
ignace Posted December 12, 2009 Share Posted December 12, 2009 I really like your title: "WHYISNT THIS MYSQL STATEMNT NOT WORKING?" So your SQL statement works IS NOT .. NOT Quote Link to comment https://forums.phpfreaks.com/topic/184597-whyisnt-this-mysql-statemnt-not-working-php/#findComment-975974 Share on other sites More sharing options...
emopoops Posted December 13, 2009 Author Share Posted December 13, 2009 lol yeah it works now. Quote Link to comment https://forums.phpfreaks.com/topic/184597-whyisnt-this-mysql-statemnt-not-working-php/#findComment-976309 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.