in_array (probably very easy to solve)

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
hanzo
Forum Newbie
Posts: 16
Joined: Thu Nov 17, 2005 10:14 am

in_array (probably very easy to solve)

Post 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!
Last edited by hanzo on Tue Dec 20, 2005 2:07 pm, edited 1 time in total.
KyleKatarN
Forum Newbie
Posts: 4
Joined: Thu Dec 08, 2005 11:36 am

Post 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
hanzo
Forum Newbie
Posts: 16
Joined: Thu Nov 17, 2005 10:14 am

Post by hanzo »

unfortunatly it doesn't work either with $row['email'] but thanks for the reply
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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";
hanzo
Forum Newbie
Posts: 16
Joined: Thu Nov 17, 2005 10:14 am

Post by hanzo »

Thanks for the explanation, helped alot :D

thanks to HAWLEYJR too :wink: :roll:
Post Reply