Query execution in codeigniter - database

I have a situation where I need to fetch details of an employee from the database using his ID and display them in the browser.
$sql = "SELECT * FROM employeesalarypayment WHERE empid = ".$val['empid'].";";
$query = $this->db->query($sql);
These are the statements that I have written to get the result array. My problem is how do I take a single field/column from this array? Also have I done it correctly?
Thanks in advance.

If your query return single data from database then you need to use
$row = $query->row_array()

Try this
$sql = "SELECT * FROM employeesalarypayment WHERE empid = ".$val['empid'].";";
$result = $this->db->query($sql)->result_array();
echo $result[0]['field_name'];

Related

how to use variable in SQL query language in WordPress

I have a DB table named 'affiliate_wp_coupons' where has 3 cols and they are;
coupon_id
affiliate_id
coupon_code
I have coupon_code which is coming from a variable, not static.
Now I wanted to query the associated affilaite_id value based on the coupon_code.
I am trying this code:
$results = $wpdb->get_results( "SELECT * FROM affiliate_wp_coupons WHERE `coupon_code`= 'BS0PDDTTGU'");
This code returns an Object with the matched col.
In the above code coupon_code value is static, how can I put a variable here?
$coupon_code = 'BS0PDDTTGU';
$results = $wpdb->get_results($wpdb->prepare("SELECT * FROM affiliate_wp_coupons WHERE `coupon_code`= %s", $coupon_code));
There're multiple ways to concatenate/inserting variable into a string in PHP, ex:
$coupon_code = 'Windy';
$str = "Hello {$coupon_code}!"; // Hello Windy!
$str2 = "Hello $coupon_code!"; // Hello Windy!
$str3 = 'Hello '.$coupon_code.'!'; // Hello Windy!
personally I prefer $str2.
Then in your case you can replace your code something like:
$results = $wpdb->get_results( "SELECT * FROM affiliate_wp_coupons WHERE `coupon_code`= '{$coupon_code}'");
Note: don't forget to define $coupon_code before run query.
PHP reference: https://www.php.net/manual/en/language.operators.string.php
Related discussion: PHP - concatenate or directly insert variables in string

Getting value from database custom table not working in wordpress

I have created a wordpress table with wp_compare_post with following column id, user_id, post_id, post_type. I have attached the image of the database.
Below is the coading for viewing data from database
function compare_counting(){
global $wpdb;
//get user id
$user_ID = get_current_user_id();
$sql = "SELECT * FROM $wpdb->compare_post WHERE user_id = '$user_ID '";
$results = $wpdb->get_results($sql) or die(mysql_error());
foreach( $results as $result ) {
echo $result->post_id.',';
}
}
But getting following error.
Notice: Undefined property: wpdb::$compare_post in
I think it could be because you need to use $wpdb->prefix
Try this:
$sql = "SELECT * FROM ".$wpdb->prefix."compare_post WHERE user_id = '$user_ID'";

How to bind pdo key and value from Question mark?

I want to make a query select data :
$pdo = new PDO($dsn, $user, $pass, $options);
$query = "SELECT ? FROM ? WHERE ? = ?";
$pdo->prepare($query);
How can I do it?
PDOpreapare statement is not support this query right row

update query sql table in yii

How can I update row in table in yii? I am using the following code but it is not working
$sql = "UPDATE auth_assignment SET itemname = 'Authenticated' WHERE userid = $user->accountID";
$command = $connection->createCommand($sql);
$command->execute();
My guess is that $user is being converted to a string and thus the ->accountID is not working. You have two methods, one unsafe and one safe.
Unsafe - Add {} around the $user->accountID.
$sql = "UPDATE auth_assignment SET itemname = 'Authenticated' WHERE userid = {$user->accountID}";
Safer - Use a parametrized query:
$sql = "UPDATE auth_assignment SET itemname = 'Authenticated' WHERE userid = :userid";
$command = $connection->createCommand($sql);
$command->execute(array(':userid' => $user->accountID))
ok I forgot to put commas around
'user->accountID'

Codeigniter - pass single query result to controller with json - if exists

Wanting to pass a single codeigniter query result to my controller using json.
my model function is:
function get_linked_loads_qty($q){
$sql = $this->db->query("
IF EXISTS(
select qtylinkedorders
from Linked_Order_Summary
join Linked_Order_lines
on Linked_Order_Summary.linkedorderid= Linked_Order_lines.linked_order_id
where load_number='$q'
)
begin
select qtylinkedorders
from Linked_Order_Summary
join Linked_Order_lines
on Linked_Order_Summary.linkedorderid= Linked_Order_lines.linked_order_id
where load_number='$q'
END
ELSE BEGIN
select 1 as qtylinkedorders
from customer_Order_lines
where CustomerOrderID='$q'
group by CustomerOrderID
END
");
$query = $this->db->get();
if($query->num_rows > 0){
foreach ($query->result_array() as $row){
$row_set[] = htmlentities(stripslashes($row[qtylinkedorders])); //build an array
}
return $row_set;
}
}
and my controller is:
function get_linked_loads_qty(){
$this->load->model('Sales_model');
if (isset($_POST['data'])){
$q = strtolower($_POST['data']);
$data = $this->Sales_model->get_linked_loads_qty($q);
$this->output->set_content_type('application/json')->set_output(json_encode($data));
}
}
The sql works 100% in mssql. the codeigniter error is:
<p>Error Number: 42000</p><p>[Microsoft][SQL Server Native Client 11.0][SQL Server]Must specify table to select from.</p><p>SELECT *</p>
as mentioned the query executes perfectly in mssql.
any assistance welcome. Thanks.
I believe the $this->db->get() is not needed, and is what causes the error. The $this->db->query("blah...") will run the query and return the result. See CodeIgniter DB Query Docs

Resources