Staggan Posted November 6, 2012 Share Posted November 6, 2012 Hello I am having a strange problem... I am running a MySQL query, and trying to pass two values from the result to a function..... it doesn't work.... it seems the variable values are not getting passed... however, if I substitute for a test variable it all works.... Below is the code. $challengerresult2 = mysql_query("SELECT * from challenge where challenge_id = $challengeid AND status = 'completed'"); $num_rows = mysql_num_rows($challengerresult2); while($row = mysql_fetch_array($challengerresult2)){ $winnerid = $row['winner_team_id']; $loserid = $row['loser_team_id']; $winnergoals = $row['winner_num_goals']; $losergoals= $row['loser_num_goals']; $challengeteamid = $row['challenger_team_id']; $opponentteamid= $row['opponent_team_id']; mail ("[email="martyn@staggan.com"]xxxx[/email]", "RESULT", $winnergoals); $testvar1 = 8; $testvar2 = 10; $KO->setResByMatch( $gamenumber, $getresultsloop, $testvar1, $testvar2); // Arguments: match index, round index, score 1, score 2. } Using testvars my function works, but if I use $winnergoals and $losergoals it doesn't. However, emailing the $winnergoals var to myself I get the right value.. Any ideas, I have been pulling my hair out with this Thanks Quote Link to comment https://forums.phpfreaks.com/topic/270352-passing-variable-from-mysql-query-to-function/ Share on other sites More sharing options...
trq Posted November 6, 2012 Share Posted November 6, 2012 Post the actual code that isn't working. Quote Link to comment https://forums.phpfreaks.com/topic/270352-passing-variable-from-mysql-query-to-function/#findComment-1390491 Share on other sites More sharing options...
Staggan Posted November 6, 2012 Author Share Posted November 6, 2012 This is the code I am trying to use: $challengerresult2 = mysql_query("SELECT * from challenge where challenge_id = $challengeid AND status = 'completed'"); $num_rows = mysql_num_rows($challengerresult2); while($row = mysql_fetch_array($challengerresult2)){ $winnerid = $row['winner_team_id']; $loserid = $row['oppomemt_team_id']; $winnergoals = $row['winner_num_goals']; $losergoals= $row['loser_num_goals']; $challengeteamid = $row['challenger_team_id']; $opponentteamid= $row['opponent_team_id']; $KO->setResByMatch( $gamenumber, $getresultsloop, $winnergoals, $losergoals); // Arguments: match index, round index, score 1, score 2. } This is the function being called public function setResByMatch($m, $r, $s1, $s2) { /* Sets a match result by specifying match number and round number. */ // Test if input is valid. if (!$this->isMatchCreated($m, $r) || // Valid round and match? $this->bracket[$r][$m]['s1'] == -1 || $this->bracket[$r][$m]['s2'] == -1 || // Are competitors "ready"/exist? !is_int($s1) || !is_int($s2) || $s1 < 0 || $s2 < 0 || $s1 == $s2) { // Valid scores? return false; } Quote Link to comment https://forums.phpfreaks.com/topic/270352-passing-variable-from-mysql-query-to-function/#findComment-1390495 Share on other sites More sharing options...
Muddy_Funster Posted November 6, 2012 Share Posted November 6, 2012 which varaibles arn't getting passed? have you echoed out all the results from the query? why are you using select *? what's the point in $num_rows? why use mysql_fetch_array() rather than mysql_fetch_assoc() when you only address the associated keys from the query result? Quote Link to comment https://forums.phpfreaks.com/topic/270352-passing-variable-from-mysql-query-to-function/#findComment-1390498 Share on other sites More sharing options...
Staggan Posted November 6, 2012 Author Share Posted November 6, 2012 OK, so $winnergoals / $losergoals are not getting passed, and I have echoed those out and emailed them to myself and they do contain number values I am using select statement to get the number of goals for each team in the tournament Actually $num_rows was used, it's not now.... I tried mysql_fetch_array and it was not working... I tried assoc because of the resource ID I was getting... and it does give me the values I want... but I am not saying I am doing it correctly... So, $winnergoals / $losergoals should be passed to setResByMatch as the 3rd and 4th parameters but that function does not seem to get the values... changing $winnergoals / $losergoals to $testvar1 and $testvar2 give the expected results Quote Link to comment https://forums.phpfreaks.com/topic/270352-passing-variable-from-mysql-query-to-function/#findComment-1390500 Share on other sites More sharing options...
Muddy_Funster Posted November 6, 2012 Share Posted November 6, 2012 since setResByMatch() is public, throw a die("$m | $r | $s1 | $s2"); in the fist line within the method and see what comes back when using the query method and the test vars. Quote Link to comment https://forums.phpfreaks.com/topic/270352-passing-variable-from-mysql-query-to-function/#findComment-1390504 Share on other sites More sharing options...
Staggan Posted November 6, 2012 Author Share Posted November 6, 2012 That die gives me the correct values, even for the vars that were not working before... and yet if I remove the die I still get no result, but I do when using either numbers or different vars... I am totally confused... Also, IE was giving me loads of garbage at one point too... had to close the window and reopen it.. but Chrome was fine... Quote Link to comment https://forums.phpfreaks.com/topic/270352-passing-variable-from-mysql-query-to-function/#findComment-1390505 Share on other sites More sharing options...
Staggan Posted November 6, 2012 Author Share Posted November 6, 2012 I've solved it.... The value coming from the dbase is a string.. (although it is an INT in the dbase) and the function requires and INT, so I have cast the done this $num1 = (int)$winnergoals and it now works.... How can I solve this without the additional cast? Quote Link to comment https://forums.phpfreaks.com/topic/270352-passing-variable-from-mysql-query-to-function/#findComment-1390508 Share on other sites More sharing options...
Muddy_Funster Posted November 6, 2012 Share Posted November 6, 2012 you could try to apply typcasting on the $row as you assign it to the variable Quote Link to comment https://forums.phpfreaks.com/topic/270352-passing-variable-from-mysql-query-to-function/#findComment-1390529 Share on other sites More sharing options...
PFMaBiSmAd Posted November 6, 2012 Share Posted November 6, 2012 Your setResByMatch() function code is not correct. is_int doesn't test if the content of a variable is an integer, it tests if the variable type is an integer. Quote Link to comment https://forums.phpfreaks.com/topic/270352-passing-variable-from-mysql-query-to-function/#findComment-1390535 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.