PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, March 19, 2022

[FIXED] Column not found: 1054 Unknown column 'Array' in 'order clause' yii

 March 19, 2022     mysql, php, yii     No comments   

Issue

I am trying to query the database using CDbCriteria in Yii 1. What I am doing is:

$criteria->addInCondition('t.id', $dealIdArr);
$criteria->order = "FIELD(t.id, $dealIdArr)";

And this is the $dealIdArr and it's not empty:

Array ( [0] => 3 [1] => 2 )

But when run the script, it returns the error:

CDbCommand failed to execute the SQL statement: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Array' in 'order clause'.

I'm unable to understand what is wrong with this? Any help?


Solution

You're currently adding an array to both conditions. I'm not too familiar with Yii, but I'm quite sure that it expects a string for the order - and not an array.

You can implode() it to convert it to a string.

$dealIdStr = implode(", ", $dealIdArr);
$criteria->addInCondition('t.id', $dealIdArr); // You can use array here
$criteria->order = "FIELD(t.id, $dealIdStr)"; // This expects a string

As your array currently only contains integers (3 and 2), you don't need to quote that - but if the array for any reason contains strings, you need to have them quoted in MySQL by doing

$dealIdStr = implode("', '", $dealIdArr);
$criteria->order = "FIELD(t.id, '$dealIdStr')";

Notice the added single-quotes.

When you try to use an array as a string, PHP will simply print "Array", which is what you're seeing now. And since its not quoted, MySQL think its a column, and not a string.

  • http://php.net/implode


Answered By - Qirel
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home
View mobile version

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing