Page 1 of 1

in_array (probably very easy to solve)

Posted: Tue Dec 20, 2005 11:40 am
by hanzo
Hi everyone,

I'm a complete php noob :) who has a problem...
I made the following code

Code: Select all

<?

$name = $_POST["name"];
$email = $_POST["email"];
$address = $_POST["address"];
$city = $_POST["city"];
$zip = $_POST["zip"];
$country = $_POST["country"];
$pass = $_POST["pass"];
$repass = $_POST["repass"];

//checking everything

//1 check password

if ( $pass != $repass ) { 
die("passwords are not the same");
};

//2 check email address

mysql_connect ('localhost','USERNAME  REMOVED BY HAWLEYJR','PASSWORD REMOVED BY HAWLEYJR')or die('Could not connect: ' . mysql_error());
mysql_select_db ('totallyj_ipod');
$query = "SELECT email FROM users WHERE activated = 1";
$result = mysql_query ($query);
if (!$result) {
   echo "Could not successfully run query ($sql) from DB: " . mysql_error();
   exit;
}
while ($row = mysql_fetch_assoc($result)) {
   echo $row["email"], "\n";
}
if (array_key_exists($email, $row)) { die ("emailaddress is already used"); };
?>
The email check doesn't work as it should, it says:
Warning: array_key_exists(): The second argument should be either an array or an object in /home/totallyj/public_html/freegiftipod/register2.php on line 31
What am I doing wrong?

Thanks alot!

Posted: Tue Dec 20, 2005 11:59 am
by KyleKatarN
Shouldnt it be like:
$row['email'] instead of just $row?
because $row doesn't have any specific value i guess.
im probably wrong though =P

Posted: Tue Dec 20, 2005 12:09 pm
by hanzo
unfortunatly it doesn't work either with $row['email'] but thanks for the reply

Posted: Tue Dec 20, 2005 1:18 pm
by Chris Corbyn
$row is a "local variable", it's only visible in the scope of that loop ;)

Either reset the result pointer and fetch the row again or store it into a global variable ;)

mysql_data_seek($result, 0)

http://uk.php.net/variables.scope -- Doesn't seem to mention loops but it does apply.

Basically, a varible initialized within the loop declaration is only available inside that loop.

An example would be:

Code: Select all

$array = array(1, 2, 4, 8, 16, 32);

foreach ($array as $base2)
{
    echo "$base2\n";
}

if (isset($base2)) echo "base2 is set";
else echo "base2 isn't set";

Posted: Wed Dec 21, 2005 6:42 am
by hanzo
Thanks for the explanation, helped alot :D

thanks to HAWLEYJR too :wink: :roll: