Can anyone spot any errors here? Sorry for asking again but I am new here and wasn't quite sure what to do when posting.
$stmt = $db->prepare('INSERT INTO `giveawayusers` (`giveawayid`, `steamid64`, `coins`, `from`, `to`) VALUES (:id, :id64, :coins, :from, :to)');
$stmt->execute(array(
":id" => $id,
":id64" => $steamUserId,
":coins" => $coins,
":from" => $currentCoins,
":to" => $totalCoins,
));
if($currentCoins+$coins>=$totalCoins)
{
$winningticket = mt_rand(1,$totalCoins);
$stmt = $db->prepare('SELECT `steamid64` FROM `giveawayusers` WHERE `from` <= :winningticket AND `to` >= :winningticket AND `giveawayid`=:id');
$stmt->bindValue(':id', $id);
$stmt->bindValue(':winningticket', $winningticket);
$stmt->execute();
$winner = $stmt->fetch();
I believe the error could be here
$stmt = $db->prepare('SELECT `steamid64` FROM `giveawayusers` WHERE `from` <= :winningticket AND `to` >= :winningticket AND `giveawayid`=:id');
My database structure is like:
https://gyazo.com/4ec4874d879b2cf4bd1d2450e57cfa71 https://gyazo.com/95cddb0a1c7b8fd1c360a6d5a1b2e129
Cheers, James
WHERE `from` >= :winningticket AND `to` <= :winningticket
I don't believe we are allowed to reference a named bind placeholder more than once in a prepared statement. (This was true in earlier versions of PDO, not sure if that's fixed in the version you are running.)
I suggest using unique bind placeholder names. The same value can be supplied for multiple placeholders. As an example:
$stmt = $db->prepare('SELECT `steamid64` FROM `giveawayusers`'
. ' WHERE `from` <= :winningticket1'
. ' AND `to` >= :winningticket2'
. ' AND `giveawayid` = :id'
);
$stmt->bindValue(':winningticket1', $winningticket);
$stmt->bindValue(':winningticket2', $winningticket);
$stmt->bindValue(':id', $id);
$stmt->execute();
But if that's the problem, then I wouldn't describe the observed behavior as an "Array error".
That description is rather vague, and misleadingly imprecise. If this is the cause of the error, then it's the execute
that is throwing the error (not the prepare) and I would expect the error message to include mention an invalid number of bound parameters" or something like that. We should include the exact error message when describing the behavior we observe, not a vague generalization.
If that's not the cause of the error, if the execute
is successful, then as @TopCheese mentions in a comment, it's valid for a query execution to return an empty resultset. In that case, I don't see how the code shown in the question would throw an "Array error".