macfall Posted July 21, 2011 Share Posted July 21, 2011 I have a database for members that looks like this: | id | username | password | supporter | info | 1 person pass yes blahblahblah 2 person2 pass no NULL 3 person3 pass no NULL 4 person4 pass yes blahblahblah where "supporter" is a enumerated field with the two values, 'yes' and 'no', and "info" is just a text field for comments. What I need to do is to create a cookie that distinguishes between supporters and non-supporters (i.e., those who have given me money and those who haven't). So I need to be able to tell which value is in the "supporter" field for a given member. It would look like this: if { setcookie("supporter"); } else { setcookie("non_supporter"); } But I don't know how to determine whether the member is a supporter. I have looked over several tutorials, and I fail to see how the SELECT query can be made to read from a particular row in a table. If I have 4 rows, and the member is on the third row, how do I make SELECT give me the value of "supporter" for that user? Quote Link to comment https://forums.phpfreaks.com/topic/242507-i-dont-understand-mysql-select/ Share on other sites More sharing options...
xkklxl Posted July 21, 2011 Share Posted July 21, 2011 If I understand your question correctly, you'd simply use WHERE. Something like: SELECT * from table WHERE username='person' Quote Link to comment https://forums.phpfreaks.com/topic/242507-i-dont-understand-mysql-select/#findComment-1245488 Share on other sites More sharing options...
macfall Posted July 21, 2011 Author Share Posted July 21, 2011 As easy as that? You'd think one of the four tutorials I looked at would have mentioned it. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/242507-i-dont-understand-mysql-select/#findComment-1245493 Share on other sites More sharing options...
xkklxl Posted July 21, 2011 Share Posted July 21, 2011 Yeah, I'm not that good with SQL either, but I test my queries on phpMyAdmin to see if it works before I use them in my code with the SQL tab. Probably not the best way, but hell, I'm learning from it. Quote Link to comment https://forums.phpfreaks.com/topic/242507-i-dont-understand-mysql-select/#findComment-1245500 Share on other sites More sharing options...
Muddy_Funster Posted July 21, 2011 Share Posted July 21, 2011 Don't use a SELECT * to get a single field...also, check the password aswell, in case there are multiple users with the same username. SELECT supporter FROM table WHERE username = 'user' AND password = 'pass' Quote Link to comment https://forums.phpfreaks.com/topic/242507-i-dont-understand-mysql-select/#findComment-1245649 Share on other sites More sharing options...
macfall Posted July 22, 2011 Author Share Posted July 22, 2011 K, looks like I'm not doing this right. This is the login script I'm using, and I'm using it to set the cookie for supporters: <?php session_start(); //Start sessions mysql_connect('localhost', 'thrrive2', 'Cf3>AzhB?P') or die ("Could not connect to mysql because ".mysql_error()); mysql_select_db('thrrive2_fledgepressusers') or die ("Could not select database because ".mysql_error()); if(mysql_num_rows(mysql_query("SELECT * FROM dbUsers WHERE username='".$_POST["username"]."' and password='".$_POST["password"]."'"))==1) { include("back.php"); mysql_query("SELECT supporter FROM dbUsers WHERE username='".$_POST["username"]."' and password='".$_POST["password"]."'"); $supporter=$_POST["supporter"]; if($supporter=="yes") { setcookie("fledge_press", time()+86400); } session_register("username"); //Create the session } else { echo 'Wrong username or password!'; include("back.php"); } ?> Login works fine, but apparently it's not setting the cookie. I use this to determine whether or not the cookie has been set: <?php session_start(); if(isset($_COOKIE['fledge_press'])) { echo "The cookie exists, " .$_POST["username"]. "."; } else { echo "The cookie does not exist, " .$_POST["username"]. "."; } ?> And even after a successful login, and having double- and triple-checked to make sure that the "supporter" field is set to "yes", the cookie does not get set. Also I think I have a problem passing the "username" value, because nothing shows up for it in the above code, no matter which message I get. Quote Link to comment https://forums.phpfreaks.com/topic/242507-i-dont-understand-mysql-select/#findComment-1246013 Share on other sites More sharing options...
macfall Posted July 22, 2011 Author Share Posted July 22, 2011 2nd-page bump. Any clues? Quote Link to comment https://forums.phpfreaks.com/topic/242507-i-dont-understand-mysql-select/#findComment-1246198 Share on other sites More sharing options...
dcro2 Posted July 22, 2011 Share Posted July 22, 2011 Look very carefully at these two lines: mysql_query("SELECT supporter FROM dbUsers WHERE username='".$_POST["username"]."' and password='".$_POST["password"]."'"); $supporter=$_POST["supporter"]; See the problem yet? Where is the value of $supporter coming from? You have to fetch a row from the query (see mysql_fetch_array) and then get the field from that array. $row = mysql_fetch_array($result); $supporter = $row['supporter']; Also, always run anything you get from $_POST or $_GET through mysql_real_escape_string before passing it through a query or somebody is bound to do this: SELECT * FROM dbUsers WHERE username='username' and password='blah' OR 1=1 Quote Link to comment https://forums.phpfreaks.com/topic/242507-i-dont-understand-mysql-select/#findComment-1246221 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.