0

I have the following error when I make a select in a temporary table:

Warning: mysql_fetch_assoc() expects parameter 1 to be resource, string given 

$row_checkbanned = mysql_fetch_assoc($query_checkbanned);

Here is the complete code:

mysql_select_db($database_config, $config);
$query_temptable = "CREATE TEMPORARY TABLE IF NOT EXISTS temp (
id int NOT NULL AUTO_INCREMENT,
player_id int(11) NOT NULL,
team_id int(11) NOT NULL,
newteam_id int(11) NOT NULL, PRIMARY KEY(id))";
$Result1 = mysql_query($query_temptable, $config) or die(mysql_error());


for($i=0; $i < count($_POST['id']); $i++){
$p_id=mysql_real_escape_string($_POST['id'][$i]);
$t_id=mysql_real_escape_string($_POST['hometeam'][$i]); 
$nt_id=mysql_real_escape_string($_POST['teamID'][$i]);
$insertSQLban = "INSERT INTO temp (player_id, team_id, newteam_id) VALUES ('$p_id', '$t_id', '$nt_id')"; 
mysql_select_db($database_config, $config);
$Result1 = mysql_query($insertSQLban, $config) or die(mysql_error());}


$query_checkbanned = ("SELECT temp.player_id FROM temp, f_banned WHERE f_banned.banplayer_id = temp.player_id AND f_banned.bteam_id = temp.team_id GROUP BY temp.player_id ORDER BY temp.player_id ASC");
$checkbanned = mysql_query($query_checkbanned, $config) or die(mysql_error());
$row_checkbanned = mysql_fetch_assoc($query_checkbanned);
$totalRows_checkbanned = mysql_num_rows($checkbanned);    

Where do I go wrong?

blips
  • 41
  • 1
  • 5
  • 2
    Try `$row_checkbanned = mysql_fetch_assoc($checkbanned);` – Funk Forty Niner Jun 09 '14 at 15:29
  • 1
    It's a matter of passing in the result resource (the return from `mysql_query()` into `mysql_fetch_assoc()`. You passed the SQL string instead. – Michael Berkowski Jun 09 '14 at 15:30
  • 1
    Meanwhile though, please review [How can I prevent SQL injection in PHP](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) and consider switching to an API supporting prepared statements, like MySQLi or PDO. The old `mysql_*()` functions are now deprecated and will be removed from PHP in an upcoming release. – Michael Berkowski Jun 09 '14 at 15:31

1 Answers1

1

You passed the wrong parameter to both mysql_fetch_assoc and mysql_num_rows

$checkbanned = mysql_query($query_checkbanned, $config) or die(mysql_error());
$row_checkbanned = mysql_fetch_assoc($checkbanned);
$totalRows_checkbanned = mysql_num_rows($row_checkbanned);  
meda
  • 45,103
  • 14
  • 92
  • 122