Valid Mysql Query Returns Nothing

I have a perfectly valid mysql query:

SELECT * FROM `defense_queue` WHERE `pid`='22'

…which returns 1 row when in phpmyadmin, but NOTHING in php.

Here is the php code which executes this query:


$where_id = mysql_query("SELECT * FROM `defense_queue` WHERE `pid` ='22'");
$where_id = $where_id[id];
echo $where_id;

and here is the SQL code which creates this table:



CREATE TABLE IF NOT EXISTS `defense_queue` (
  `id` int(11) NOT NULL auto_increment,
  `uid` int(11) NOT NULL,
  `pid` int(11) NOT NULL,
  `s1` varchar(225) NOT NULL default '0' COMMENT 'defense_id,number,timestarted',
  `s2` varchar(225) NOT NULL default '0',
  `s3` varchar(225) NOT NULL default '0',
  `s4` varchar(225) NOT NULL default '0',
  `s5` varchar(225) NOT NULL default '0',
  `s6` varchar(225) NOT NULL default '0',
  `s7` varchar(225) NOT NULL default '0',
  `s8` varchar(225) NOT NULL default '0',
  `s9` varchar(225) NOT NULL default '0',
  `s10` varchar(225) NOT NULL default '0',
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=4 ;

Before you ask, I have looked far and wide already for a solution to this problem on many forums on the internet. Every one I could come across did not solve the problem.

$where = mysql_query(“SELECT * FROM defense_queue WHERE pid =‘22’”);
$where_row = mysql_fetch_assoc($where);
$where_id = $where_row[‘id’];
echo $where_id;

[fphp]mysql_query[/fphp] returns a result resource, not an array. Also no need for single quotes around ‘22’ if it’s a numeric data type.

          while ($row=mysql_fetch_array($result)) {
              $array_of_values[]=$row;
          }
          return $array_of_values;
      }

That will loop through the results set and add each row of the results set to an array in turn. If your going to use a foreach loop act on the array then just before the above code add:

$array_of_values=array();

That will set up as an array so that if there are no matches, the foreach loop will still see $array_of_values as being an array, albeit an empty one.