Can I create a conditional using a column from my SQL table?

First time employing a database, so bear with me. Basically, in my index.php file, two options are put head-to-head in a “battle.”

In my database, I have added a column that specifies a “type” for each entry. I want to create an if statement so that only the type apples competes with apples, and oranges compete with oranges (so my entry in the database will be “apples” or “oranges” for each entry).

Right now, in the index.php is just pulling a random entry from $images for option A and option B, like this:

[php]

[/php]

So if there is a column in $images with entries like “apples” and “oranges,” can I create a conditional statement to ensure like-types are placed against each other? Any advice would be much appreciated!

If it helps, here is the code that I execute in SQL to create the table in the database (also, if it helps, I’ve attached a screenshot of what the output table looks like after the code is executed).

[php]CREATE TABLE IF NOT EXISTS battles (
battle_id bigint(20) unsigned NOT NULL auto_increment,
winner bigint(20) unsigned NOT NULL,
loser bigint(20) unsigned NOT NULL,
PRIMARY KEY (battle_id),
KEY winner (winner)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

CREATE TABLE IF NOT EXISTS `images` (
    `image_id` bigint(20) unsigned NOT NULL auto_increment,
    `filename` varchar(255) NOT NULL,
    `score` int(10) unsigned NOT NULL default '1500',
    `wins` int(10) unsigned NOT NULL default '0',
    `losses` int(10) unsigned NOT NULL default '0',
    PRIMARY KEY  (`image_id`)
    ‘player_name’ varchar(255) NOT NULL,
    ‘player_opponent’ varchar(255) NOT NULL,
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;[/php]

Won: <?=$images[0]->wins?>, Lost: <?=$images[0]->losses?> Won: <?=$images[1]->wins?>, Lost: <?=$images[1]->losses?>

I don’t think you can really tell MySQL how to store the rows. You can query for different things though.

Sponsor our Newsletter | Privacy Policy | Terms of Service