CodeIgniter Forums
SQL function - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: SQL function (/showthread.php?tid=6457)



SQL function - El Forum - 02-28-2008

[eluser]piker[/eluser]
Can I use SQL functions in query selection?

Code:
$query = $this->db->query("SELECT path,title, SUBSTR( body, 1, 50 )  FROM news ");


Is it other way to resolve the problem?


Have nice day
piker


SQL function - El Forum - 02-28-2008

[eluser]xwero[/eluser]
You should be able to run that query, which error are you getting? To debug you can add
Code:
echo $this->db->last_query();
To see if/where CI messes with the query.


SQL function - El Forum - 02-29-2008

[eluser]piker[/eluser]
Solution
I made in VIEW something like this:

Code:
<?php foreach ($adam as $key): ?>
  <h3>&lt;?=$key->title?&gt;</h3>
  <p>&lt;?=substr($key->body, 0, 100)?&gt;...</p>

etc...

You ask me for an error information.
If in controller is something like this:

Code:
line1 $query = $this->db->query("SELECT title, SUBSTR( body, 1, 50 )  FROM newsy");
line2
line3 foreach ($query->result_array() as $row)
line4 {
line5    echo $row['title'];
line6    echo $row['body'];
line7 }


CI give us that error information:

Quote:A PHP Error was encountered

Severity: Notice

Message: Undefined index: body

Filename: controllers/newsy.php

Line Number: 6

I'am using XAMPP under WinXP in standard configuration.


SQL function - El Forum - 02-29-2008

[eluser]xwero[/eluser]
you have to use an alias for the field in the function and it can't be the same as the field name.
Code:
$query = $this->db->query("SELECT title, SUBSTR( body, 1, 50 ) as short  FROM newsy");
And then you have to use the alias to show the manipulated field.