Welcome Guest, Not a member yet? Register   Sign In
How to use AS keyword and Aliases sql with query()
#1

(This post was last modified: 02-20-2021, 06:09 AM by venance.)

Hi, I have a problem with query() when trying to execute my statement with "AS" keyword, Seem like renaming db table column like this
Model:
public function test(){
$this->db->query("SELECT column AS name FROM table");
return $this;
}

Controller
$data= ['test' => $this->model->test()->paginate(5)];

Views
<?php foreach ($test as $row): ?>
<?= $row['name']; ?>
<?php endforeach; ?>

When I use $row['column'] It works fine, but when I use $row['name'] I get error msg undefined "name".
How can I fix this issue?
Reply
#2

@venance,

Did you verify that the output from the query used the field names that you used in your query? Did you run the query (in phpMyAdmin/or database tool) to confirm that the field names you created are in the result set?
Reply
#3

(02-20-2021, 10:02 AM)php_rocs Wrote: @venance,

Did you verify that the output from the query used the field names that you used in your query?  Did you run the query (in phpMyAdmin/or database tool) to confirm that the field names you created are in the result set?
Yes I did that to phpMyadmin the statement is fine but the problem is on output.
Reply
#4

(02-20-2021, 10:02 AM)php_rocs Wrote: @venance,

Did you verify that the output from the query used the field names that you used in your query?  Did you run the query (in phpMyAdmin/or database tool) to confirm that the field names you created are in the result set?
When I run sql to phpMyadmin I get result looks like this:
   +-----------+
   |  name     |
   +-----------+
   |  value1    |
   |  value2    |
   +-----------+

And If I use  <?= $row->column ?> I get all the data, But if I use AS "newcolumnname" I get back that error.
Reply
#5

(This post was last modified: 02-20-2021, 10:51 AM by iRedds.)

The query() method immediately runs a database query. But you don't get the result.
Use the select() method instead
PHP Code:
->select('column as name'false); 
Reply
#6

(02-20-2021, 10:49 AM)iRedds Wrote: The query() method immediately runs a database query. But you don't get the result.
Use the select() method instead
PHP Code:
->select('column as name'false); 
Ok but I want to write my SQL statement because in some case I can write complex and wrong sql without having a need to chain alots of methods.
When I use:
'test'=>$this->model->test()->asObject()->paginate(1), 

and output this is work fine
<?= $row->column ?>

But the problem if I try to use  renamed column  "name". It throw an error "Undefined property: stdClass::$name"
Reply
#7

(02-20-2021, 10:49 AM)iRedds Wrote: The query() method immediately runs a database query. But you don't get the result.
Use the select() method instead
PHP Code:
->select('column as name'false); 
 Did paginate() method ignore the use of AS keyword when trying to rename db table field/column?
Reply
#8

(02-20-2021, 11:50 AM)venance Wrote:  Did paginate() method ignore the use of AS keyword when trying to rename db table field/column?

I will try to explain

When you call in the test method this code.
PHP Code:
$this->db->query("SELECT column AS name FROM table"); 
You are not getting the result of this query. 

The paginate method in this line 
PHP Code:
$this->model->test()->paginate(5

Makes two queries to the database:
1. Calls ths countAllResults() method.
2. Calls the findAll() method. 

And since you don't define any conditions, then ... you get requests like.

SELECT COUNT() FROM table 
SELECT * FROM table LIMIT 5 OFFSET 0 <-- You will receive the result of this request.

But if in the test() method you would use the code.

PHP Code:
public function test()
{
  $this->db->select('column AS name'false)->where('a''b');
  return $this;


Then the requests would look different

SELECT COUNT() FROM table WHERE a = b
SELECT column as name FROM table WHERE a = b LIMIT 5 OFFSET 0

If you have such a complex query that you cannot compose with QueryBuilder, then it is easier for you to override the paginate method in your model.
Reply
#9

(This post was last modified: 02-20-2021, 09:42 PM by venance.)

I get result if I use paginate() and output <? $row['column']?>.
But the issue I get if I try to use a renamed table field <? $row['name']?>.
I get error " Undefined property: stdClass::$name".
I cant figure out what cause this issue.
I current using ci4

(02-20-2021, 09:15 PM)iRedds Wrote:
(02-20-2021, 11:50 AM)venance Wrote:  Did paginate() method ignore the use of AS keyword when trying to rename db table field/column?

I will try to explain

When you call in the test method this code.
PHP Code:
$this->db->query("SELECT column AS name FROM table"); 
You are not getting the result of this query. 

The paginate method in this line 
PHP Code:
$this->model->test()->paginate(5

Makes two queries to the database:
1. Calls ths countAllResults() method.
2. Calls the findAll() method. 

And since you don't define any conditions, then ... you get requests like.

SELECT COUNT() FROM table 
SELECT * FROM table LIMIT 5 OFFSET 0 <-- You will receive the result of this request.

But if in the test() method you would use the code.

PHP Code:
public function test()
{
  $this->db->select('column AS name'false)->where('a''b');
  return $this;


Then the requests would look different

SELECT COUNT() FROM table WHERE a = b
SELECT column as name FROM table WHERE a = b LIMIT 5 OFFSET 0

If you have such a complex query that you cannot compose with QueryBuilder, then it is easier for you to override the paginate method in your model.

With method I get error "Undefined index: name"
Code:
findAll()

But if I use findAll() with
Code:
$row['column']

I get all the data
Reply
#10

(02-20-2021, 09:31 PM)venance Wrote: I get result if I use paginate() and output <? $row['column']?>.
But the issue I get if I try to use a renamed table field <? $row['name']?>.
I get error " Undefined property: stdClass::$name".
I cant figure out what cause this issue.
I current using ci4

Your test method doesn't affect pagination in any way.
For this reason, you get the error. Pagination knows nothing about your "as name"

PHP Code:
$data= ['test' => $this->model->test()->paginate(5)];
// Add this code and you will receive the last request to the database. 
dd($this->model->getLastQuery()); 
Reply




Theme © iAndrew 2016 - Forum software by © MyBB