Passing 2 strings through array to the where clause in codeigniter - arrays

I want to send 2 strings through array from controller to model and get the results from db, but there is a problem I'm facing.
My controller is like:
$data = array();
if($query = $this->authors_model->get_authors_list(array('author_Type' => array('admin', 'author'))))
{
$data['authors'] = $query;
}
My Model :
function get_authors_list($options = array())
{
if(isset($options['author_Type']))
$this->db->where('author_Type', $options['author_Type']);
$this->db->order_by('author_Id', 'ASC');
$query = $this->db->get('mg_authors');
return $query->result();
}
and the error I'm getting:
A PHP Error was encountered
Severity: Notice
Message: Array to string conversion
Filename: database/DB_active_rec.php
Line Number: 427
Error Number: 1054
Unknown column 'Array' in 'where clause'
SELECT * FROM (mg_authors) WHERE author_Type = Array ORDER BY
author_Id ASC LIMIT 15
Filename: D:\xampp\htdocs\sport\system\database\DB_driver.php
Line Number: 330

You need to use WHERE IN when you put array. In CodeIgniter, it need to do like this:
$this->db->where_in('author_Type', $options['author_Type']);

Related

Unknown column 'value' in 'where clause'

I have reviewed every post and tried a few recommendations but nothing is working. My code has been fine -- but Gravity Forms did an update and messed up the database tables the data was pulling from so I edited that but the data is still not pulling properly and I am getting the error: Unknown column 'value' in 'where clause' when I turn on debug.
Can anyone see why I am getting this error? The page should be retrieving a list of team members based on the value they have entered in a graavity form field.....all was fine until the update.
Here is the code that is causing the issues - specifically the $get_team_ids and $user_email lines seem to be the offenders.
function showstep1(){
$concate='';
$postid = get_the_ID();
$post_7 = get_post($postid);
if( is_mr() ) {
//print_r($post_7->post_title);
}
//echo $postid;
global $wpdb;
$table_name = $wpdb->prefix . "walking_steps";
$table_name1 = $wpdb->prefix . "users";
$table_name2 = $wpdb->prefix . "gf_entry_meta";
$get_team_ids = $wpdb->get_results("SELECT * FROM $table_name2 where value = '" .$post_7->post_title. "'");
if( is_mr() ) {
//print_r($get_team_ids);
//print_r( $getuserids );
}
$users = array();
if(!empty($get_team_ids)){
foreach ($get_team_ids as $idlead) {
$user_email = $wpdb->get_var("SELECT value FROM wp_gf_entry_meta WHERE form_id=4 AND lead_id=$idlead->lead_id AND field_number=3");
if( empty( $user_email ) ) {
$user_email = $wpdb->get_var("SELECT value FROM wp_gf_entry_meta WHERE form_id=1 AND lead_id=$idlead->lead_id AND field_number=2");
}
if( $user_email ) $users[] = $user_email;
//$concate .= $idlead->lead_id.',';
}
$concate = substr($concate, 0,-1);
//echo $concate;
}
Unknown column 'value' in 'WHERE clause' would usually indicate that the column does not exist in the table.
You say that this is also happening for the other line 'user_email' which also is trying to SELECT the column 'value' again, if this causing you a problem, the column name is not correct it would seem.
Is it the correct header for the column? Seems like an ambiguous name. Try renaming the column 'value' to something less generic or change the where clause to
"WHERE table_name2.value = "
If this is not the column header, please replace 'value' with the correct column header
EDIT: Correct Code below as per comments
Replace all instances of 'value' with 'meta_value'

How to get values between two given values in Cakephp 3

I want to get product with specified price range supplied using the below query but getting following error:
Cannot convert value to string InvalidArgumentException
$product_List=$this->Products->find('all',array('conditions' => array('product_price BETWEEN ? and ?' => array($min, $max))));
Here product_price is of type varchar(100). Please help to sort out my issue.
I think it's help you
$product_List = $this->Products->find('all')
->where(function ($exp) use($min,$max) {
return $exp->between('product_price', $min, $max); //Consider $min=100 ; $max =1000; product_price BETWEEN 100 AND 1000;
});
Another way.
$product_List = $this->Products->find('all')
->where(['product_price <=' => $max, 'product_price >=' => $min]);

How to check whether value is in database without throwing an exception in Zend?

I have a registration action in Zend controller and I want to ensure that given username isn't already in the database.
$tUser = $userMapper->getDbTable();
$select = $tUser->select()
->from(array('u'=>'users'))
->where('u.user_username = ' . $value);
$row = $tUser->fetchRow($select);
Unfortunately, this snippet of code throws the Exception when i just want to check the $row is null or not and keep writing my code.
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'admin' in 'where clause', query was: SELECT u.* FROM users AS u WHERE (u.user_username = admin) LIMIT 1
Should I remove the try/catch blocks in my controller or can I do it in another way?
Judging by error description, I'd say it will throw an exception always, no matter if user exists or not, because bad SQL query will be generated. Try passing $value as second parameter to where, like this:
$tUser = $userMapper->getDbTable();
$select = $tUser->select()
->from(array('u'=>'users'))
->where('u.user_username = ?', $value);
$row = $tUser->fetchRow($select);
You will then check if user exists like this:
if(!$row) {
// user doesn't exist
} else {
// user exists
}
try {
$tUser = $userMapper->getDbTable();
$select = $tUser->select()
->from(array('u'=>'users'))
->where('u.user_username = ?', $value);
$row = $tUser->fetchRow($select);
} catch (Zend_Db_Exception $e) {
echo "<pre>";print_r($e);
}

Generate wrong "Update" query cakephp

I am trying to update data using below code but it is generating wrong SQL query. don't understand why it is generating wrong one. Please help me out.
$data = array();
$data['Pers']['etat'] = 1;
$data['Pers']['Activation'] = '';
$this->Pers->id = $results['Pers']['persID'];
$this->Pers->save($data);
Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'bdr+.pers.persID' in
'where clause'
SQL Query: UPDATE `bdr+`.`pers` SET `etat` = 1, `Activation` = '',
`date_time` = '2014-03-19 11:33:21' WHERE `bdr+.pers.persID` = 37
It means bdr+.pers.persID is generating wrong. Don't understand why it is generating like this.
check if this $results['Pers']['persID'] variable if it is returning a proper format
then try this
$data = array();
$data['Pers']['persID'] = $results['Pers']['persID'];
$data['Pers']['etat'] = 1;
$data['Pers']['Activation'] = '';
//Just to make sure your model is loaded
$this->loadModel('Pers');
if ($this->Pers->save($data, false)) {
echo 'Pres has been saved :) ';
}
Your code containing bdr+.pers.persID` = '37' because $results['Pers']['persID'] supplying 37

when i use findby($id) with association conditions i get SQL Error: 1054

i use this variable to find the comments that related with articles..
$comment = $this->Article->Comment->findAllById($id);
i get errors when i added conditons like this..
$comment = $this->Article->Comment->findAllById($id,array('conditions' => array('Comment.status' => 1)));
i see this error>>
Warning (512): SQL Error: 1054: Unknown column 'Comment.' in 'field list' [CORE\cake\libs\model\datasources\dbo_source.php, line 684]
Query: SELECT DISTINCT `Comment`.`` FROM `comments` AS `Comment` LEFT JOIN `articles` AS `Article` ON (`Comment`.`article_id` = `Article`.`id`) WHERE `Comment`.`id` = 15
Take a look at the manual for the findAllBy<field_name>() method.
You'll notice the second parameter is an array of fields.
I would recommend using Cake's standard find method, something like:
$comment = $this->Article->Comment->find('all', array(
'conditions' => array(
'Comment.id' => $id,
'Comment.status' => 1
)
));

Resources