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;

?

Naveed Hasan
  • 641
  • 7
  • 19

2 Answers2

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.

Michael Mior
  • 28,107
  • 9
  • 89
  • 113
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;
Clemens
  • 186
  • 3
  • Thanks! It works. But can if I want to protect it, should I use it with quotes,double-quotes or backticks? – Naveed Hasan Feb 02 '12 at 14:43
  • If you look for a way to prevent bad userdata in your SQL queries look here http://stackoverflow.com/questions/3797613/sql-injection-and-codeigniter or google. – Clemens Feb 02 '12 at 14:46