Welcome Guest, Not a member yet? Register   Sign In
SQL function
#1

[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
#2

[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.
#3

[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.
#4

[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.




Theme © iAndrew 2016 - Forum software by © MyBB