Unable to display results in YII query Builder

I am facing an issue with using two different syntaxes of the Yii query builder. The first syntax works perfectly but I cannot use variables inside the SQL.

$connection = Yii::app()->db;

$sql='SELECT id_activo, nom_activo, valor_activo, cod_amenaza, tipo_activo, deg_amenaza, 
frec_amaneza FROM activo, amenazas WHERE amenazas.activos_amenaza ILIKE "%"|| activo.tipo_activo ||"%" AND id_activo=1';
$query = str_replace('"',"'",$sql);

$command = $connection->createCommand("$query");
$rows = $command->queryAll();

However, the second syntax does not display any results.

$i=1;
$rows = Yii::app()->db->createCommand()
->select('id_activo, nom_activo, valor_activo, cod_amenaza, tipo_activo, deg_amenaza, frec_amaneza')
->from('activo, amenazas')
->where('amenazas.activos_amenaza LIKE :substr', array(':substr' => '%activo.tipo_activo%'))
->andWhere('id_activo=:id', array(':id'=>$i))
->queryAll();

If I remove the where condition with the LIKE clause, it works fine.

Answer №1

The issue at hand is that '%activo.tipo_activo%' is being interpreted as a plain string rather than data from the database.

To address this, you can adjust the join condition in the following way:


    $rows = Yii::app()->db->createCommand()
    ->select('id_activo, nom_activo, valor_activo, cod_amenaza, tipo_activo, deg_amenaza, frec_amaneza')
    ->from('activo')
    ->join('amenazas', "amenazas.activos_amenaza LIKE '%'||activo.tipo_activo||'%' ")<br>
    ->where('id_activo=:id', array(':id'=>$i))
    ->queryAll();

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The optimal method for determining if a file is an image in PHP

Looking for a more efficient way to print logos from an external URL? I currently have a code that checks if a file is an image or not. This is the code I'm using right now: if(@is_array(getimagesize($mediapath))){ $image = true; } else { $i ...

Removing the key from the output of FOR JSON PATH in SQL 2016: A step-by-step guide

Lately, we have incorporated the usage of FOR JSON PATH in our SQL code for various reasons. However, I am currently facing a slight issue in a specific scenario. My aim is to obtain a JSON string containing a list of OrderItemIds. Instead, what I am getti ...

Changing the width of an HTML table does not produce any noticeable results

I attempted to create a striped table with 5 columns. The desired column sizes are: 10% width --- 10% width --- 40% width --- 20% width --- 20% width However, my current code is not achieving the correct widths (the 'description' column does n ...

The setInterval() function is not functioning properly when used with PHP's file_get_contents

Currently, I'm encountering an issue while attempting to use the function get_file_contents() within a setInterval(). The objective is to continuously update some text that displays the contents of a file. Below is the code snippet: <script src="h ...

CakePHP: Oops! The view for the TasksController::index() cannot be located

I am currently using CakePHP version 2.5.5 for my project located in the directory: C:\xampp\htdocs\vy\cakephp-2.5.5. The layout of my project directory is as follows: I have created a file named C:\xampp\htdocs\vy&b ...

Error encountered during payment processing: User authentication failed as a result of invalid authentication values provided

After clicking on the payment link provided at https://github.com/AuthorizeNet/sdk-php, I encountered an issue with the message "User authentication failed due to invalid authentication values". Surprisingly, the code works perfectly for the developer acc ...

Storing Form Data in MySQL, upon clicking the submit button the user is redirected to a PHP page and the information is not

Currently, I am facing an issue with injecting data into my SQL table. Whenever I input information into the forms and click on the submit button, the page redirects me to my PHP file without actually injecting any data. The tables remain empty. Below is ...

Using database queries to populate a series of interconnected drop-down menus

Currently, I am facing an issue with my 2 drop-down menus. The first one is populated with possible continents, and the second should display countries based on the continent selected in the first menu. However, all countries are showing up in the dropdown ...

PHP does not detect the Ajax object that has been sent to it

Here's the data I am trying to send: In this blockquote: switch=rssAdd&data=[object Object],[object Object]... etc. This data is generated by the following code snippet: The Javascript function shown above parses XML elements and formats them ...

What steps can be taken to properly display dateTime values in a data table when working with JavaScript (VueJS) and PHP (Laravel)?

I am facing an issue where I am unable to save user inputted date-time values from a modal into a data table. Despite receiving a success message, the dateTime values are not being added to the table. My payload only displays the state and approval fields ...

Trouble arises when attempting AJAX call to PHP file resulting in failed MySQL execution accompanied by errors

Having some trouble identifying the issue here. It seems quite simple, but I'll highlight it here anyway. On a basic website, I have a form for user registration where I'm using AJAX to send data to register.php. The form and AJAX code are as fol ...

Transmitting an array via Ajax

Is there a way to pass an array from JavaScript to PHP using Ajax? var selectedCheckboxes = $('.def-mask :checkbox:checked').serialize(); $.ajax({ url: 'ajax/battle.php', type: 'post', data: { playerReady: 1, atta ...

Bringing PHP Back into the World of Ajax

I created a form for users to upload new avatars and now I am experimenting with using AJAX to display error messages on the same page if any occur. However, my current issue is that the AJAX call is not receiving any response from the PHP file it's c ...

What is the process for incorporating Express.js variables into SQL queries?

Recently delving into the world of node.js, I've embarked on a journey to create a login and registration system using express.js, vanilla JS, CSS, HTML, and MySql. Within the following code lies the logic for routing and handling HTTP Post requests ...

Issue with TLS connectivity when connecting to the Mosquitto broker using Mosquitto-PHP

Trying to establish a connection between my PHP MQTT client and the broker, but encountering an error: Fatal error: Uncaught exception 'Mosquitto\Exception' with message 'A TLS error occurred.' in /path/testemqtt.php:27 Stack ...

What is the process for subtracting two data values in MySQL?

I am using the following code to calculate a sum. My question is, how can I subtract the data from total_bayaran and total_denda in this code? select translator_name, project_name, SUM(payment) AS total_payment, SUM(fine) AS TOTAL_fine FROM tbl_episode ...

Tips for establishing a connection between two articulate models

I am handling two Models: Users and Appointments class Appointments extends Model { use HasFactory; protected $fillable = [ 'business_id', 'user_id', 'subject', 'description', ...

Capture locations from Google Maps

What is the best method to extract all addresses, along with their latitude and longitude, for a specific city (such as Bangalore) from Google Maps using PHP/JavaScript and store it in a MySQL table? I urgently need assistance. Thank You ...

Looking for a way to optimize query performance within a loop? Concerned about memory constraints?

I have created a custom shopping cart component for my Joomla website. Users are buying codes that we store in our database, which are linked to physical incentive cards. During checkout, I need to retrieve the purchased codes from the database and then up ...

Ensure proper escaping of PHP special characters in Elementor and WordPress integration

I'm attempting to add this code into Elementor's Custom Code section in order to modify the Sale Badge Text on WooCommerce add_filter(‘woocommerce_sale_flash’, ‘edit_sale_badge’); function edit_sale_badge() { return ‘TEXT-GOES-HERE’; ...