clankill3r Posted April 10, 2011 Share Posted April 10, 2011 Hi, i got this function to get the wordCount that i store in a tabel: function getWordCount($word) { $query = "SELECT count FROM uniqueWords WHERE word='$word'"; $data = execQuery($query); return $data; } Only it returns an array, what i want is to have it return a integer, how can i do that? Quote Link to comment https://forums.phpfreaks.com/topic/233264-return-a-int-from-a-function/ Share on other sites More sharing options...
PFMaBiSmAd Posted April 10, 2011 Share Posted April 10, 2011 Without the definition of what execQuery() does, what you data in the table is, or what the array looks like now, it will be a little hard to specifically help you. I'll guess you could try - return $data['count']; Quote Link to comment https://forums.phpfreaks.com/topic/233264-return-a-int-from-a-function/#findComment-1199635 Share on other sites More sharing options...
clankill3r Posted April 10, 2011 Author Share Posted April 10, 2011 sorry, forgot that it was not a bis php function: function execQuery($query) { $result = mysql_query($query) or die ("Query failed: " . mysql_error()); $data = array(); while($row = mysql_fetch_array($result, MYSQL_ASSOC)) { $data[] = $row; } return $data; } With return $data['count']; i get: Notice: Undefined index: count in /home/vhosting/x/vhost0033377/domains/doeke.nl/htdocs/www/artez/uitwerking01/cms/lib.php on line 99 It can also happen that the word not exists for that i should return zero. I now got this but it's still returning an array function getWordCount($word) { $query = "SELECT count FROM uniqueWords WHERE word='$word'"; $data = execQuery($query); if(count($data) > 0){ return $data[0]; } else { return 0; } } Quote Link to comment https://forums.phpfreaks.com/topic/233264-return-a-int-from-a-function/#findComment-1199639 Share on other sites More sharing options...
PFMaBiSmAd Posted April 10, 2011 Share Posted April 10, 2011 You are returning an array of arrays. Use the following to see what you are getting - echo '<pre>',print_r($data,true),'</pre>'; Quote Link to comment https://forums.phpfreaks.com/topic/233264-return-a-int-from-a-function/#findComment-1199661 Share on other sites More sharing options...
clankill3r Posted April 10, 2011 Author Share Posted April 10, 2011 i get stuff like this (small part of whole): Array ( ) Array ( ) Array ( [0] => Array ( [count] => 1 ) ) Array ( [0] => Array ( [count] => 1 ) ) [17] => Array ( [count] => 1 ) [18] => Array ( [count] => 1 ) Quote Link to comment https://forums.phpfreaks.com/topic/233264-return-a-int-from-a-function/#findComment-1199736 Share on other sites More sharing options...
spiderwell Posted April 10, 2011 Share Posted April 10, 2011 is count a column name?, if so perhaps put it in backticks SELECT `count` from uniqueWords WHERE word='$word'; [/count] Quote Link to comment https://forums.phpfreaks.com/topic/233264-return-a-int-from-a-function/#findComment-1199747 Share on other sites More sharing options...
clankill3r Posted April 10, 2011 Author Share Posted April 10, 2011 did you also mean [/count]? Cause with that i get a error. And if i use: $query = "SELECT 'count' FROM uniqueWords WHERE word='$word'"; i get: Array ( [0] => Array ( [count] => count ) ) Array ( [0] => Array ( [count] => count ) ) Quote Link to comment https://forums.phpfreaks.com/topic/233264-return-a-int-from-a-function/#findComment-1199749 Share on other sites More sharing options...
spiderwell Posted April 10, 2011 Share Posted April 10, 2011 haha no i meant [ / code] not count sorry but use BACKTICKS ```````````` NOT single quote '''''''''''''''' means very different things in SQL are you trying to count rows in a database or select a column named count ? Quote Link to comment https://forums.phpfreaks.com/topic/233264-return-a-int-from-a-function/#findComment-1199753 Share on other sites More sharing options...
clankill3r Posted April 10, 2011 Author Share Posted April 10, 2011 With the backticks i get: Array ( ) Array ( ) Array ( [0] => Array ( [count] => 1 ) ) I have a database where i want to store all unique words from a text, and the count. So count is a column name. Quote Link to comment https://forums.phpfreaks.com/topic/233264-return-a-int-from-a-function/#findComment-1199757 Share on other sites More sharing options...
spiderwell Posted April 10, 2011 Share Posted April 10, 2011 function getWordCount($word) { $query = "SELECT `count` FROM uniqueWords WHERE word='$word'"; $result = mysql_query($query ); while($row=mysql_fetch_array($result)) { $return $row['count']; } return "nothing found"; } } Quote Link to comment https://forums.phpfreaks.com/topic/233264-return-a-int-from-a-function/#findComment-1199761 Share on other sites More sharing options...
clankill3r Posted April 10, 2011 Author Share Posted April 10, 2011 thaks that was almost correct I got it working now. Quote Link to comment https://forums.phpfreaks.com/topic/233264-return-a-int-from-a-function/#findComment-1199765 Share on other sites More sharing options...
spiderwell Posted April 10, 2011 Share Posted April 10, 2011 good stuff in futures its worth noting that SQL has reserved names like COUNT so you will need to refer to them with backticks because COUNT is actually a function in mysql interesting reading Quote Link to comment https://forums.phpfreaks.com/topic/233264-return-a-int-from-a-function/#findComment-1199766 Share on other sites More sharing options...
clankill3r Posted April 10, 2011 Author Share Posted April 10, 2011 thx, good to know Quote Link to comment https://forums.phpfreaks.com/topic/233264-return-a-int-from-a-function/#findComment-1199769 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.