Jump to content

$gender returns '1' but not M or F as in the record


Recommended Posts

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

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.

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

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. 

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.

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.