Hi Guys,
I really don't not know where I'm going wrong with this. I am learning PHP. I have been able to input a user birthday on registration on my website using a list menu such as is on facebook. I however just cant be able to extract the date, month and year separately if i need to.

MySQL "users" table(only a portion)

username user_birthday
-------- -------------
kkjay 1975-04-16
gregory 1955-09-27

My PHP code is:

<?php require_once('Connections/connections.php'); ?>

mysql_select_db($database_connections, $connections);
$query_birthdate = "SELECT day(user_birthday) FROM users WHERE username='kkjay'";
$birthdate = mysql_query($query_birthdate, $connections) or die(mysql_error());
$row_birthdate = mysql_fetch_assoc($birthdate);
echo $row_birthdate;

I keep getting the word array when I echo the result instead of getting 16.

I would really appreciate any help on how to solve this. Thanks in advance.

Recommended Answers

All 3 Replies

Mysql returns an array with the individual database fields/columns as elements of the array. If you don't have a copy of the PHP user manual, then you need it. You can get one here.

Once you have the birth date from the database, then you can turn it into a time stamp using strtotime and then extract what you need using date.

$query_birthdate = "SELECT day(user_birthday) as Day FROM users WHERE username='kkjay'";
$birthdate = mysql_query($query_birthdate, $connections) or die(mysql_error());
$row_birthdate = mysql_fetch_assoc($birthdate)

echo $row_birthdate['Day'];

Always use the mysql functions in your queries when you can, as you have done, the database server has that functionality for a reason. No reason to reinvent the wheel.

This only works because you're returning 1 row in the query. If you were returning multiple rows, you would have to loop over the $result to get each row's value.

Finally, if you're working with mysql 4.1 or greater you should NOT be using the mysql extension and really should be using the mysqli or PDO extensions to access the database. These allow access to the newer features provided by the mysql database server.

Thank you very much Chrishea and Mschroeder for your quick replys. It works now. Great help and I hope to become as proficient as you all are.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.