phillyrob0817;11046027 wrote:
and (Ball1 = $b or Ball2 = $b or Ball3 = $b or Ball4 = $b or Ball5 = $b)");
Not knowing what Balls 1 to 5 are, I'm still guessing you need to normalize your table by creating a separate ”ball” relation. Not knowing the contents of $table, I will call that relation ”tbl”. Because ”date” is a reserved word in SQL, I have replaced that identifier by ”created”. If you stick with ”date” you will have to quote it according to your database. The SQL standard delimiter for quoting identifiers is " (double quote). MySQL default is ` back-tick (which is convenient to use when creating SQL statements in PHP.
create table ball (
id int unsigned primary key auto_increment, -- this would be 1 - 5, one entry for each of the previous Ball1 to Ball5 fields
-- possibly other fields
);
You would then also need a table that links tbl to ball
create table tbl_ball (
tbl_id int unsigned,
ball_id int unsigned,
FOREIGN KEY (tbl_id) REFERENCES tbl(id),
FOREIGN KEY (ball_id) REFERENCES ball(id),
PRIMARY KEY (tbl_id, ball_id)
);
This would now turn your select statement into
SET @b := 2;
SET @date := '2015-01-01';
SELECT count(*) AS mycount1
FROM tbl
WHERE created >= @date AND EXISTS (SELECT 1 FROM tbl_ball WHERE tbl_id = tbl.id AND ball_id = @b);
It may look more complicated than your approach, but it solves a lot of problems in the long run.
Also: +1 for prepared statements.