4 Replies - 6987 Views - Last Post: 30 July 2004 - 03:21 AM Rate Topic: -----

#1 jonic  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 30-May 03

Passing An Array Into A Switch Statement...

Posted 29 July 2004 - 08:27 AM

hi,

not been here for a while. php's going pretty well for me recently, but today i hit a snag with a switch statement...

i have the following code:

function checkUser($username, $password) {
  global $result;
  
  $q = mysql_query("SELECT * FROM org_users WHERE username='" . $username . "'");
  list($org_ID, $q_username, $md5user, $q_password, $activated, $contact_firstname, $contact_lastname, $orgName, $emailAddress, $phoneNumber) = mysql_fetch_array($q);

  $data = array($username, $password, $q_username, $q_password);
  
  switch ($data) {
 	 case !$data[0]:
    $result = 'noUser';
    break;
 	 case !$data1[1]:
    $result = 'noPass';
    break;
 	 case !$data[2]:
    $result = 'badUser';
    break;
 	 case $data[3] != $data[1]:
    $result = 'badPass';
    break;
 	 default:
    $result = 'success';
 	 }
  echo($result . '<br />');
  return $result;
	}


the $result is coming out fine, but unless i've not inputted the username it always returns "noPass", even when there's a password there...

I think it's possibly because the switch statement can't read arrays, which doesn't make sense as an array is an array...

can anyone help me out with this or should i eschew the switch statement in favor of "if/else if"s?

someone please help... it took me a whole do figure our the "global $result;" bit... i don't want to waste anymore time on this really...

any help is appreciated...

cheers,

jonic.

Is This A Good Question/Topic? 0
  • +

Replies To: Passing An Array Into A Switch Statement...

#2 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: Passing An Array Into A Switch Statement...

Posted 29 July 2004 - 09:24 AM

Check out this page. Arrays can't be used in a switch unless they're dereferenced. I don't think objects can be used either in a switch.
Was This Post Helpful? 0
  • +
  • -

#3 MK_PRE__  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 477
  • Joined: 12-January 02

Re: Passing An Array Into A Switch Statement...

Posted 29 July 2004 - 09:28 AM

switch statements can read any var providing that each case you supply is of the same type, so having an array for the switch and then say an integr or string for the case wont work

also, it looks as though you are trying to code a user login of some sort, theres actually a much easier way to do it and should save you a lot of time and frustration.

this should work, not sure if best way but its the way i use now, i think its pretty good providing your secure your vars


// make sure you secure your vars in the select statement first so it isnt vulnerable to sql injection

$result=mysql_query("SELECT * FROM org_users WHERE username='$user' AND password='$pass'");
$row=mysql_fetch_array($result);
if (mysql_num_rows($result)>0){
  echo "success";
} else {
     echo "bad user/pass";
}



Was This Post Helpful? 0
  • +
  • -

#4 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: Passing An Array Into A Switch Statement...

Posted 29 July 2004 - 09:53 AM

Matt Kasper, on Jul 29 2004, 11:28 AM, said:

switch statements can read any var providing that each case you supply is of the same type,

Good point! I did not know that about PHP. Thanks for the info!
Was This Post Helpful? 0
  • +
  • -

#5 jonic  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 30-May 03

Re: Passing An Array Into A Switch Statement...

Posted 30 July 2004 - 03:21 AM

nice one, thanks...

next question... is there any way to pass more than one variable into a switch statement?

when i tried:
switch ($var1, $var2, $var3, $var4){
...
}


php throws back an error complaining about an unexpected "," in the switch... is there anyway at all to do this?

also, what did you mean by dereferenced?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1