EchoFool Posted March 8, 2010 Share Posted March 8, 2010 Hey - i was told that mysql_fetch_assoc($Query); Is automatically an array... So i tried in a function to do return ( mysql_fetch_assoc($Query); Then did a "in_array" against the function but it doesn't work. When i print the array in the function i get "ArrayArrayArray" This is how i do the in array: <?php function completedresearch($User){ //query to get all rows in user/research table which have been completed assigned to $Query return(mysql_fetch_assoc($Query) } //general stuff here If(in_array($ResearchID,completedresearch($_SESSION['Current_User'])){ Echo 'Research completed'; }Else{ Echo 'Not Completed'; } ?> Hope you can explain where im going wrong because it should be saying "Research Completed" but it doesn't. Link to comment https://forums.phpfreaks.com/topic/194546-mysql-fetch-assoc-array/ Share on other sites More sharing options...
Anti-Moronic Posted March 8, 2010 Share Posted March 8, 2010 Not sure if this is a syntax error due to retyping here but this line is wrong: return(mysql_fetch_assoc($Query) ..which can and should be: return mysql_fetch_assoc($Query); Along with that, the variable $Query is not accessible via this function because it is not within scope. You will need to pass that along with the user: completedresearch($Query, $_SESSION['Current_User']) and change your function: function completedresearch($Query, $User){ So, completed, should look like so: <?php function completedresearch($Query, $User){ //query to get all rows in user/research table which have been completed assigned to $Query return mysql_fetch_assoc($Query); } //general stuff here If(in_array($ResearchID,completedresearch($Query, $_SESSION['Current_User'])){ Echo 'Research completed'; }Else{ Echo 'Not Completed'; } ?> Hope that helps. Link to comment https://forums.phpfreaks.com/topic/194546-mysql-fetch-assoc-array/#findComment-1023223 Share on other sites More sharing options...
Anti-Moronic Posted March 8, 2010 Share Posted March 8, 2010 Another thing, there is another syntax error in your if statement which needs another ) on the end. Link to comment https://forums.phpfreaks.com/topic/194546-mysql-fetch-assoc-array/#findComment-1023226 Share on other sites More sharing options...
EchoFool Posted March 8, 2010 Author Share Posted March 8, 2010 Oh yeh i just retyped to get the idea accorss i get no syntax errors its just a logic error.. You kinda got confused with my function ill type it out full: <?php function completedresearch($User){ $Query = mysql_query("SELECT RecordID FROM researchusers WHERE Completed='1' AND UserID='$UserID' ORDER BY ResearchID ASC") or die(mysql_error()); return mysql_fetch_assoc($Query); } //general stuff here If(in_array($ResearchID,completedresearch($_SESSION['Current_User'])){ Echo 'Research completed'; }Else{ Echo 'Not Completed'; } ?> Hope this now explains a bit better Link to comment https://forums.phpfreaks.com/topic/194546-mysql-fetch-assoc-array/#findComment-1023228 Share on other sites More sharing options...
EchoFool Posted March 8, 2010 Author Share Posted March 8, 2010 bump Link to comment https://forums.phpfreaks.com/topic/194546-mysql-fetch-assoc-array/#findComment-1023257 Share on other sites More sharing options...
EchoFool Posted March 9, 2010 Author Share Posted March 9, 2010 bump Link to comment https://forums.phpfreaks.com/topic/194546-mysql-fetch-assoc-array/#findComment-1023309 Share on other sites More sharing options...
gizmola Posted March 9, 2010 Share Posted March 9, 2010 Look at your param: function completedresearch($User){ The name is $User. Now look at the variable that you are using in the query. Is that that same variable name? You can figure these things out with a simple echo statement, but I have to point out that you don't actually check to see if the mysql_query returns a valid result set. If you make that type of assumption, when things go wrong you get a runtime error, because fetching against a 'false/null' result set causes a runtime error. Link to comment https://forums.phpfreaks.com/topic/194546-mysql-fetch-assoc-array/#findComment-1023317 Share on other sites More sharing options...
EchoFool Posted March 9, 2010 Author Share Posted March 9, 2010 Okay fixed that but same problem persists. When i print array i get ArrayArrayArray (3 in total) - wouldn't that suggest that it returned something though ? Because if it did not the array would be empty? Link to comment https://forums.phpfreaks.com/topic/194546-mysql-fetch-assoc-array/#findComment-1023325 Share on other sites More sharing options...
EchoFool Posted March 9, 2010 Author Share Posted March 9, 2010 Ok turns out it doesn't automatically make an array with mysql_fetch_assoc. Thats a shame Link to comment https://forums.phpfreaks.com/topic/194546-mysql-fetch-assoc-array/#findComment-1023335 Share on other sites More sharing options...
Anti-Moronic Posted March 9, 2010 Share Posted March 9, 2010 Ok turns out it doesn't automatically make an array with mysql_fetch_assoc. Thats a shame I use it all the time and works fine for me..? what does mysql_fetch_array produce if you use that instead here? print_r($Query); If your query has produced a mysql resource it will throw that into an associative array with mysql_fetch_assoc. In fact, once of the only differences is that mysql_fetch_array is twice in size because it produces both an associative array and regular array. Link to comment https://forums.phpfreaks.com/topic/194546-mysql-fetch-assoc-array/#findComment-1023341 Share on other sites More sharing options...
gizmola Posted March 9, 2010 Share Posted March 9, 2010 mysql_fetch_assoc() most certainly does return an array, when it actually has something to fetch. It returns exactly what the name implies -- an array keyed by the names of the column. You have exactly one column in your select statement, so the array for one fetch would look something like: $row = array('RecordID' => ?); From your code, I'm not sure what you're trying to do, since I don't really understand your database design. What I can say is this -- when you fetch one row, you get one row. That is not by any means the entire result SET, it is only the first row -- and in this case a single value. If you're expecting a whole series of rows, your code is not accounting for that. You also don't want an array of arrays with a single key/value pair, since you're trying to use in_array() apparently to try and find the value. This would be more likely the code you would want: function completedresearch($User){ $Query = mysql_query("SELECT ......") or die(mysql_error()); $rows = array() while ($row = mysql_fetch_assoc()) { $rows[] = $row['RecordID']; } return $rows; } Again just my best guess, given the information you've provided to this point, which is far from clear. I don't know why you do a query that references one column, even though your initial query tried to ORDER BY another column, and then seems to look for another value. It's about as clear as mud, and also begs the question of why your query doesn't figure this out more directly... the database is perfectly able to return rows or not based on WHERE criteria, so I can't figure out why you would want to pull out a result set and then search through it, when the database is better and faster at doing that itself, and just giving you the exact result set specified. Link to comment https://forums.phpfreaks.com/topic/194546-mysql-fetch-assoc-array/#findComment-1023350 Share on other sites More sharing options...
EchoFool Posted March 9, 2010 Author Share Posted March 9, 2010 Ignore this post just noticed post previous. Now thats a good explaination ! Now i understand thank you ! Link to comment https://forums.phpfreaks.com/topic/194546-mysql-fetch-assoc-array/#findComment-1023423 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.