Page 1 of 1

mysql qurey

Posted: Wed Dec 03, 2003 11:17 am
by dull1554
i am working on a forum script, i want it to check the database for the user's age, the user's username is stored in a cookie when they login, i want to query the database for age where username is the cookie walue

my code:

forum.php

Code: Select all

<?php

include "db.php";
$coppa_age = "12";
$db_userage = mysql_query("SELECT age FROM users WHERE username='$_COOKIE['complexscripting']'");
$db_age = mysql_fetch_array($db_userpass);
$db_age = $db_age['age'];
if ($age > $coppa_age) {
    include "forum_areas.php";
}
else{
    echo "you are under 13, according to coppa guidelines you are not allowed to use the forums!";
}
?>
any help would be greatly appreciated

Posted: Wed Dec 03, 2003 11:26 am
by mrvanjohnson
I assume you are gettting an error? A good debug method would be to echo your SELECT statment to make sure it's what you want. Also I noticed this

Code: Select all

<?php
$db_userage = mysql_query("SELECT age FROM users WHERE username='$_COOKIE['complexscripting']'"); 
$db_age = mysql_fetch_array($db_userpass); 
?>
shouldn't that be

Code: Select all

<?php
$db_userage = mysql_query("SELECT age FROM users WHERE username='$_COOKIE['complexscripting']'"); 
$db_age = mysql_fetch_array($db_userage); 
?>
Also you might need to change this

Code: Select all

<?php
$db_userage = mysql_query("SELECT age FROM users WHERE username='$_COOKIE['complexscripting']'"); 
?>
to this

Code: Select all

<?php
$db_userage = mysql_query("SELECT age FROM users WHERE username='".$_COOKIE['complexscripting']."'"); 
?>
That's a " ' " at the end there.

Posted: Wed Dec 03, 2003 11:31 am
by dull1554
yeah i see my mistake now, and its a darn stupid one, i copied a majority of the code from another page of mine and i dod not correct correctly. Thanks mrvanjohnson

Posted: Wed Dec 03, 2003 11:31 am
by Chambrln
Or you can just remove the single quotes from the $_COOKIE array.

Code: Select all

<?php
$db_userage = mysql_query("SELECT age FROM users WHERE username='$_COOKIE['complexscripting']'"); 

?>
to this:

Code: Select all

<?php
$db_userage = mysql_query("SELECT age FROM users WHERE username='$_COOKIE[complexscripting]'"); 
?>

Posted: Wed Dec 03, 2003 11:32 am
by mrvanjohnson
Not a problem :-)

rreply

Posted: Wed Dec 03, 2003 2:52 pm
by dull1554
this is the code that i used to get it to work;

forum.php

Code: Select all

<?php

include "../db.php";
$coppa_age = "12";


if ($_COOKIE["complexscriptingloginvalue"] == "loggedin"){
          $db_userage = mysql_query("SELECT age FROM users WHERE username='$_COOKIE[complexscriptinguser]'");
          $db_age = mysql_fetch_array($db_userage);
          $db_age = $db_age['age'];
          if ($db_age > $coppa_age)
             {
             include "forum_areas.php";
             }
else{
    echo "you are under 13, according to coppa guidelines you are not allowed to use the forums!";
}
    }
else{
          echo "please login!";
    }



?>

Posted: Wed Dec 03, 2003 5:02 pm
by d3ad1ysp0rk
wouldnt it be easier to use $_COOKIE['loggedin'] instead of ['complexscriptingloginvalue']?? and put it as a boolean (true or false)

ie.

Code: Select all

if($_COOKIE['loggedin'] == true){
//do stuff
} else {
//make them log in
}

Posted: Wed Dec 03, 2003 5:22 pm
by dull1554
yes it would, thanks