mysql qurey

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
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

mysql qurey

Post 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
User avatar
mrvanjohnson
Forum Contributor
Posts: 137
Joined: Wed May 28, 2003 11:38 am
Location: San Diego, CA

Post 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.
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

Post 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
Chambrln
Forum Commoner
Posts: 43
Joined: Tue Dec 02, 2003 10:45 am
Location: Oregon

Post 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]'"); 
?>
User avatar
mrvanjohnson
Forum Contributor
Posts: 137
Joined: Wed May 28, 2003 11:38 am
Location: San Diego, CA

Post by mrvanjohnson »

Not a problem :-)
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

rreply

Post 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!";
    }



?>
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post 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
}
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

Post by dull1554 »

yes it would, thanks
Post Reply