Array question.. easy

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Array question.. easy

Post by s.dot »

I'm getting a "in_array(): bad argument for second parameter" error for this:

Code: Select all

$date = date("Ymd");
$alreadyvotedsql = "SELECT whovoted FROM faceoffvotes WHERE faceoffid = '$faceoffid' AND date = '$date'";
$alreadyvotedquery = mysql_query($alreadyvotedsql);
$alreadyvotedarray = mysql_fetch_array($alreadyvotedquery);
if(in_array($_COOKIEї'username'], $alreadyvotedarray)){ echo "You have already voted on this faceoff matchup today.  You can vote again tommorow"; require 'footer.php'; die(); }
}
The array when printed to the browser is "Array."
How can I fix this to be a list of names from the query?
thegreatone2176
Forum Contributor
Posts: 102
Joined: Sun Jul 11, 2004 1:27 pm

Post by thegreatone2176 »

you need a variable after you fetch the array

$alreadyvotedusername=$alreadyvotedarray["username"];

which will pick the username out of that row
djot
Forum Contributor
Posts: 313
Joined: Wed Jan 14, 2004 10:21 am
Location: planet earth
Contact:

Post by djot »

-
Should be

Code: Select all

$alreadyvotedarrayї'whovoted']
then

Code: Select all

if(in_array($_COOKIEї'username'], $alreadyvotedarrayї'whovoted']))
{
  echo "You have already voted on this faceoff matchup today.  You can vote again tommorow";
  require 'footer.php';
  die();
}
djot
-
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

er

Post by s.dot »

err still having troubles

do I need to implode() my variable to get it comma separated or something?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

how's the data stored in this table? What do you expect $alreadyvotedarray to be?
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

...

Post by s.dot »

I expect it to be a list of names

such as user1, user2, user3, user4, user5, user6 etc...
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

it will not be as such. You retrieve 1 row from the table for every record that matches when you call mysql_fetch_* .. since it looks like you are searching for a particular user, why not restrict the query to that user?
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

ah

Post by s.dot »

ah, indeed

I don't know why I didn't do that. Brain fart moment I guess.

Thanks.
Post Reply