nomadsoul Posted January 15, 2011 Share Posted January 15, 2011 Hi Freaks, The code below works fine except the $gender varable echos the number one. I've set the gender record as varchar in mysqladmin and even changed it to text and char but I always get the same result of 1. Why can't I get it to return M or F? I've also used mysql_fetch_asscoc. I've been away from sql for awhile and have forgotten a bit. Thanks for your valuable time. <?php require ("connect.php"); $write= mysql_query("INSERT INTO people VALUES('','John','Smith','1980-4-28','M')"); $extract = mysql_query("SELECT * FROM people"); while ($row = mysql_fetch_array($extract)) { $id = $row['id']; $firstname = $row['firstname']; $lastname = $row['lastname']; $dob = $row = $row['dob']; $gender = $row['gender']; echo "$firstname $lastname was born on $dob and is $gender <br>"; } ?> And here is the html results with every refresh: John Smith was born on 1980-04-28 and is 1 Quote Link to comment https://forums.phpfreaks.com/topic/224501-gender-returns-1-but-not-m-or-f-as-in-the-record/ Share on other sites More sharing options...
PFMaBiSmAd Posted January 15, 2011 Share Posted January 15, 2011 Have you looked directly in your database table using your favorite database management tool to see exactly what is in your table? You might have a previous row with the value 1 and the INSERT query in the code you posted is failing (you don't have any error checking/error reporting logic in your code so that you would know if the INSERT query was working or not) and you are seeing data displayed by your code that is already in your table, not data that is the result of the code you are running. Quote Link to comment https://forums.phpfreaks.com/topic/224501-gender-returns-1-but-not-m-or-f-as-in-the-record/#findComment-1159730 Share on other sites More sharing options...
nomadsoul Posted January 15, 2011 Author Share Posted January 15, 2011 I looked in mysqladmin and the cmd as well. I also deleted all records, checked datatypes and started over. I know the query is working because I can enter different values for firstname, lastname, and dob and get different results in the php page code is in but gender always turns up 1. However, I did use the mysql_num_rows function to test my select query(which worked). $numrows = mysql_num_rows($extract); but I commented it out. Let me try eliciting an error as you suggest. If nothing else, I can always drop the table and make a new one and see if it happens again. Thanks for the reply Quote Link to comment https://forums.phpfreaks.com/topic/224501-gender-returns-1-but-not-m-or-f-as-in-the-record/#findComment-1159742 Share on other sites More sharing options...
PFMaBiSmAd Posted January 15, 2011 Share Posted January 15, 2011 Any chance that your 'gender' column is THE name of the auto-increment column? What's the actual table definition? Quote Link to comment https://forums.phpfreaks.com/topic/224501-gender-returns-1-but-not-m-or-f-as-in-the-record/#findComment-1159746 Share on other sites More sharing options...
nomadsoul Posted January 15, 2011 Author Share Posted January 15, 2011 the DESCRIBE people: query returns: id int(11) autoincrement and PK firstname is varchar(20) lastname is varchar(20) dob is date(Y-m-d) gender varchar(1) pretty much a simple table all records not null mysql_error returns nothing I also droped the table and started from scratch but still the same problem. I've look around and I'm surprised that no one else has encountered this. It's not important that I have the gender field for the website I'm trying to build but it concerns me that this error is so persistent. Quote Link to comment https://forums.phpfreaks.com/topic/224501-gender-returns-1-but-not-m-or-f-as-in-the-record/#findComment-1159756 Share on other sites More sharing options...
PFMaBiSmAd Posted January 15, 2011 Share Posted January 15, 2011 So, is the 'M' actually stored in the table when you looked directly at the data? You have got to pin down at what point your code and data is doing what you expect and at what point it is not. I suspect that the length you are using (1) is causing a problem when php tries to retrieve the data. Quote Link to comment https://forums.phpfreaks.com/topic/224501-gender-returns-1-but-not-m-or-f-as-in-the-record/#findComment-1159767 Share on other sites More sharing options...
mikosiko Posted January 15, 2011 Share Posted January 15, 2011 this line in your code: $dob = $row = $row['dob']; is most likely causing the problem Quote Link to comment https://forums.phpfreaks.com/topic/224501-gender-returns-1-but-not-m-or-f-as-in-the-record/#findComment-1159948 Share on other sites More sharing options...
Pikachu2000 Posted January 15, 2011 Share Posted January 15, 2011 this line in your code: $dob = $row = $row['dob']; is most likely causing the problem Oh, good catch! Quote Link to comment https://forums.phpfreaks.com/topic/224501-gender-returns-1-but-not-m-or-f-as-in-the-record/#findComment-1159960 Share on other sites More sharing options...
PFMaBiSmAd Posted January 15, 2011 Share Posted January 15, 2011 LOL And believe it or not, that doesn't generate any php error when trying to access $row['gender'] after doing that. $row['gender'] is returning $row[0], the first character of the dob string value that was copied to $row. Quote Link to comment https://forums.phpfreaks.com/topic/224501-gender-returns-1-but-not-m-or-f-as-in-the-record/#findComment-1159982 Share on other sites More sharing options...
nomadsoul Posted January 16, 2011 Author Share Posted January 16, 2011 $dob = $row = $row['dob']; OMG my face is red Yes that was it. Now it works. Thanks for the catch and the other insights Quote Link to comment https://forums.phpfreaks.com/topic/224501-gender-returns-1-but-not-m-or-f-as-in-the-record/#findComment-1160012 Share on other sites More sharing options...
mikosiko Posted January 16, 2011 Share Posted January 16, 2011 I also think it might have something to do with varchar(1) nothing ... AFAIK Quote Link to comment https://forums.phpfreaks.com/topic/224501-gender-returns-1-but-not-m-or-f-as-in-the-record/#findComment-1160016 Share on other sites More sharing options...
nomadsoul Posted January 16, 2011 Author Share Posted January 16, 2011 mikosiko Thanks for that PFMaBiSmAd Thanks for the valued tips. Also, it did insert 'M' and 'F' into the db Quote Link to comment https://forums.phpfreaks.com/topic/224501-gender-returns-1-but-not-m-or-f-as-in-the-record/#findComment-1160021 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.