padams Posted August 21, 2007 Share Posted August 21, 2007 Can anyone please explain this error to me? Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/sites/site138/web/ottersdb/confirmmatch3.php on line 149 I set up the record set using the following code ($players is an array): $players = $_POST['matchTeam']; $playerGames = "SELECT * FROM players WHERE playerID IN (". implode(", ",$players). ")"; $playerGames_query = mysql_query($playerGames) or die(mysql_error()); $rsPlayerGames = mysql_fetch_assoc($playerGames_query); Then I want to show the results on screen using: <?php echo $rsPlayerGames['playerID']; ?> <?php echo $rsPlayerGames['playerFirstName']; ?> <?php echo $rsPlayerGames['playerLastName']; ?> <?php echo $rsPlayerGames['playerGames']; ?> <?php } while ($rsPlayerGames = mysql_fetch_assoc($playergames_query)) ?> (the last line is the line 149 mentioned in the error) Any suggestions as to what I've done wrong? I'm new to this stuff and seem to be taking two steps backwards for every one step forward! Quote Link to comment https://forums.phpfreaks.com/topic/65941-mysql_fetch_assoc-error/ Share on other sites More sharing options...
dhruvasagar Posted August 21, 2007 Share Posted August 21, 2007 Well I think you need to do this instead of what your doing... $players = (array)$_POST['matchTeam']; Quote Link to comment https://forums.phpfreaks.com/topic/65941-mysql_fetch_assoc-error/#findComment-329654 Share on other sites More sharing options...
padams Posted August 21, 2007 Author Share Posted August 21, 2007 Thanks, but didn't change the outcome, still same error. I did forget to mention however, that the output is returning one row from the recordset, but then the error occurs. Quote Link to comment https://forums.phpfreaks.com/topic/65941-mysql_fetch_assoc-error/#findComment-329671 Share on other sites More sharing options...
pranav_kavi Posted August 21, 2007 Share Posted August 21, 2007 Print out your query and check if the query is valid. In the code u ve givn,r u using a do-while loop or just a while loop? Quote Link to comment https://forums.phpfreaks.com/topic/65941-mysql_fetch_assoc-error/#findComment-329687 Share on other sites More sharing options...
padams Posted August 21, 2007 Author Share Posted August 21, 2007 How do I print out a query? Sorry, very new to this! I missed a line of code when I pasted it in. I am trying to do a 'do while' loop on the recordset, but it keeps falling down after one result is displayed. Quote Link to comment https://forums.phpfreaks.com/topic/65941-mysql_fetch_assoc-error/#findComment-329692 Share on other sites More sharing options...
pranav_kavi Posted August 21, 2007 Share Posted August 21, 2007 try, $players = $_POST['matchTeam']; $playerGames = "SELECT * FROM players WHERE playerID IN (". implode(", ",$players). ")"; echo "query: ".$playerGames; $playerGames_query = mysql_query($playerGames) or die(mysql_error()); ......(rest of the code)... Quote Link to comment https://forums.phpfreaks.com/topic/65941-mysql_fetch_assoc-error/#findComment-329696 Share on other sites More sharing options...
dhruvasagar Posted August 21, 2007 Share Posted August 21, 2007 echo $playerGames; or print $playerGames; and then check whether your query is being formed properly. Quote Link to comment https://forums.phpfreaks.com/topic/65941-mysql_fetch_assoc-error/#findComment-329697 Share on other sites More sharing options...
padams Posted August 21, 2007 Author Share Posted August 21, 2007 It showed: query: SELECT * FROM players WHERE playerID IN (1, 2) (I selected the first two items in the list) So from what I've picked up it seems to be missing hyphens around the values 1 and 2. In another post someone gave me the following code to implode the $players variable and put commas in there, but it hasn't put hyphens in. $playerGames = "SELECT * FROM players WHERE playerID IN (". implode(", ",$players). ")"; Any idea how I could modify this to get the hyphens? (If they actually necessary.) I've played around with it but keep getting knocked back. Quote Link to comment https://forums.phpfreaks.com/topic/65941-mysql_fetch_assoc-error/#findComment-329703 Share on other sites More sharing options...
pranav_kavi Posted August 21, 2007 Share Posted August 21, 2007 do u want ur query to be of the form SELECT * FROM players WHERE playerID IN (-1, -2) ? did u mean hyphens or single quotes arnd i.e '1','2' ? Quote Link to comment https://forums.phpfreaks.com/topic/65941-mysql_fetch_assoc-error/#findComment-329707 Share on other sites More sharing options...
padams Posted August 21, 2007 Author Share Posted August 21, 2007 Doh! Meant single quotes. Not thinking straight as I've been looking at this screen for far too long! I want the query to look like: query: SELECT * FROM players WHERE playerID IN ('1', '2') Or at least that's what I think it should look like based on the stuff I read about IN. Quote Link to comment https://forums.phpfreaks.com/topic/65941-mysql_fetch_assoc-error/#findComment-329711 Share on other sites More sharing options...
pranav_kavi Posted August 21, 2007 Share Posted August 21, 2007 but that depends on the datatype of the column-playerID. If playerID is of numeric type in db,then query wud b SELECT * FROM players WHERE playerID IN (1, 2) If playerID is of varchar type in db,then query needs 2 b SELECT * FROM players WHERE playerID IN ('1', '2') Quote Link to comment https://forums.phpfreaks.com/topic/65941-mysql_fetch_assoc-error/#findComment-329725 Share on other sites More sharing options...
padams Posted August 21, 2007 Author Share Posted August 21, 2007 The primary key is integer, so I guess that having (1,2) should work then. Something else must be causing the problem, as I hard-coded in a player name into the IN parameter and it only returned one record and the same error - even though there are two players with names matching the parameter. $playerGames = "SELECT * FROM players WHERE playerFirstName IN ('mark')"; There are two records in the database which have 'mark' as their value in the playerFirstName column, so I thought I'd get two records coming back when I did this query. Is IN even the correct thing to use here? I was using it because I thought it was necessary when I was sending multiple values in the query. $playerGames = "SELECT * FROM players WHERE playerID IN (". implode(", ",$players). ")"; Or could I use playedID = (". implode(", ",$players). ")? Quote Link to comment https://forums.phpfreaks.com/topic/65941-mysql_fetch_assoc-error/#findComment-330360 Share on other sites More sharing options...
pranav_kavi Posted August 22, 2007 Share Posted August 22, 2007 i suppose u can try, $playerGames = "SELECT * FROM players WHERE playerFirstName like '%mark%'; Quote Link to comment https://forums.phpfreaks.com/topic/65941-mysql_fetch_assoc-error/#findComment-330613 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.