0

If I have a MySQL statement like this:

$sql="SELECT `name`, DATE(`time`), FROM `students` WHERE `id`='5'";

I am using CodeIgniter and trying to fetch the result in object format rather than array format (i.e. not using row_array())

$query=$db1->query($sql);
$row=$query->row();

I can get the name as

echo $row->name;

I cannot get the value of date with this method.

Any Ideas how I can get the date with this method, with something like:

echo $row->date;

?

2 Answers 2

1

You should be able to use your own field names in SQL like so:

$sql="SELECT `name`, DATE(`time`) as mydate, FROM `students` WHERE `id`='5'";

and subsequently access it like

echo $row->mydate;
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! It works. But can if I want to protect it, should I use it with quotes,double-quotes or backticks?
If you look for a way to prevent bad userdata in your SQL queries look here stackoverflow.com/questions/3797613/… or google.
1

If you want the column in the result set to be named date, then add AS date after the DATE expression. You'll then be able to access it as $row->date.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.