This String Variable Always Gives Me Zero When Inserted In MySQL - php

I have this PHP function :
public function addInvoice ($order_id, $invoice_id, $product_id, $product_quantity, $product_price, $product_reseller_fee,
$product_reseller_poin, $product_referral_fee, $product_referral_id,
$product_vendor_sales, $custom_invoice){
$this->connect();
$this->select('shopping_cart', 'courier_cost', NULL, 'product_id = "'.$product_id.'" AND order_id = "'.$order_id.'"', NULL, '1');
$ongkir = $this->getResult();
$ongkir = $ongkir[0]['courier_cost']; //-- this gives me ZERO when inserted into MySQL
$product_vendor_income = ($product_vendor_sales * $product_quantity) + $ongkir; //-- $ongkir in this formula also count as ZERO
$data = array ('order_id' => $this->escapeString($order_id),
'invoice_id' => $this->escapeString($invoice_id),
'product_id' => $this->escapeString($product_id),
'product_quantity' => $this->escapeString($product_quantity),
'product_price' => $this->escapeString($product_price),
'product_reseller_fee' => $this->escapeString($product_reseller_fee),
'product_reseller_poin' => $this->escapeString($product_reseller_poin),
'product_referral_fee' => $this->escapeString($product_referral_fee),
'product_referral_id' => $this->escapeString($product_referral_id),
'product_vendor_sales' => $this->escapeString($product_vendor_income),
'custom_invoice' => $this->escapeString($custom_invoice));
$this->insert('sales_invoice', $data);
$res = $this->getResult();
return $res;
}
$ongkir variable result is string(4) "6000" when I do var_dump. so, I believe it's a string.
on the other side, I have a MySQL colum which has Decimal data type. when I tried to insert $ongkir into database, it ends up with zero.
do I have to convert this string into numeric data type? that's odd...
I tried to show on the screen what's the value of $ongkir :
$ongkir = $product->addInvoiceCopy ('ORD - 5762 5CF0 BFB9 6', null, '801', '1', null, null, null, null, null, null, '22000', null);
$total = (100 * 2) + $ongkir;
var_dump($total);
it produce :
int(6200)
so, $ongkir is not an empty variable or zero. but why I get zero when inserted into MySQL? any clue? thank you.

Related

How to retrieve row data that has null value and one specific value on one column

I want to retrieve the row data from table course where program_vss has null and 2 value.
return $this->db->get_where('course', array('is_top_course' => 1, 'status' => 'active', 'program_vss'=> null , ));
any suggestions on how to retrieve data with two conditions
you can use an array and pass the array:-
Associative array method:-
$array = array('name' => $name, 'title' => $title, 'status' => $status);
$this->db->where($array);
// Produces: WHERE name = 'Joe' AND title = 'boss' AND status = 'active'

Do not change mysql column value if request variable is null in laravel eloquent

I need to check if $request->link_description is null then don't change anything or better to say don't touch the database column value.
is it possible to do so without doing an extra select query?
for example :
/** update ad Seat info */
$Update_Seat = AdSeat::where('id', $id)->where('owner_id', $owner_id)
->update(['seat_name' => $seat_name,
'seat_url' => $seat_url,
'logo_src' => ($logo_src) ? $logo_src : null,
'category_id' => $category_id,
'plan' => $plan,
'price_sale' => $price_sale,
'link_description' => ($link_description) ? $link_description : null,
'description' => ($description) ? $description : null,
'income_percentage' => $income_percentage,
'status' => STATUS_TO_APPROVE]);
I mean this part :
'link_description' => ($link_description) ? $link_description : null,
instead of null I want to put something like :
'link_description' => ($link_description) ? $link_description : AdSeat::whereId(5)->value('link_description),
But as I said before I'm looking for a way to not to run extra select query like above.
It would be easier to build your data array before hand. Example:
// Do not add $link_description here
$data = [
'seat_url' => $seat_url,
...
];
// But add it if $link_description is not null
if (!is_null($link_description)) {
$data['link_description'] = $link_description;
}
$Update_Seat = AdSeat::...->update($data);

check value if null return 0 otherwise return the value

What is the shortest way to set if status is null return '0' else return the value.
$value['status'] = null;
$STATUSES = array(
0 => 'Pending',
1 => 'Accepted',
2 => 'Suspended',
3 => 'Rejected',
4 => 'Waiting list',
5 => 'Terminated',
9 => 'Application in Progress'
);
$status = $STATUSES[$value['status']];
echo $status;
I can use empty() function to check if value is 'null' but I wanted to know if there is smarter way.
I am looking for a native function like this.
$status = $STATUSES[ valueToZiro( $value['status'] )];
function valueToZiro($val){
return empty($val)?0:$val;
}
If you are sure for getting an integer for your $value['status'] you can cast it to integer, it would be shorter:
// in case of null, casting will return 0
$status = $STATUSES[(int)$value['status']];
PHP automatically converts a 'null' index to an empty string
So, adding an empty string ass key which has a value of '0', fixes your problem without the need to check whether $value['status'] is null.
$STATUSES = array(
'' => '0',
1 => 'Accepted',
// etc
);
Whether this is a desirable method depends on your situation, I suppose...
See 'Arrays with NULL keys' for more information on 'null keys'
return is_null($status)?0:$status;
Using empty will also return true on empty string $var = "" , on empty arrays $var = array() and on 0. Using is_null is the safest way to check for a null value.
EDIT: Although this answer above answers your primary question, your question does not fit your code... To fix your code, I'd do:
$value['status'] = null;
$STATUSES = array(
0 => 'Pending',
1 => 'Accepted',
2 => 'Suspended',
3 => 'Rejected',
4 => 'Waiting list',
5 => 'Terminated',
9 => 'Application in Progress'
);
define('STATUSES', $STATUSES);
if(isset($STATUSES[$value['status']])){
echo $STATUSES[$value['status']];
}else{
return 0;
}
if (array_key_exists($value['status'],$STATUSES) {
return $STATUSES[$value['status']];
}
return 0;

how to encode json array from mysql table using php

<?php
$db = new PDO('mysql:host=localhost:3306;dbname=DB1;', 'user1','123456');
$sql = 'SELECT * FROM events';
$out = array();
foreach($db->query($sql) as $row) {
$out[] = array(
'id' => $row->title,
'title' => $row->name,
'url' => $row->url,
'class' => $row->class,
'start' => $row->start . '000',
'end' => $row->end .'000'
);
}
echo json_encode(array('success' => 1, 'result' => $out));
exit;
?>
Table Structure
Field Type Null Key Default Extra
id int(5) NO NULL
title text NO NULL
url text NO NULL
class text NO NULL
start datetime NO NULL
end datetime NO NULL
It displays output as
{"success":1,"result": [{"id":null,"title":null,"url":null,"class":null,"start":"000","end":"000"}, {"id":null,"title":null,"url":null,"class":null,"start":"000","end":"000"}]}
i want to print data in tables instead of null
Thank You.
You are not querying your rows the proper way.
Instead of :
'id' => $row->title,
use
'id' => $row['title'],
for more information, see example 1 in the official documentation
not tried this but you could change this:
foreach($db->query($sql) as $row) {
to
foreach($db->query($sql) as (object)$row) {
... which might allow the syntax used to access records.

Print MySQL query result from PHP

I'm trying to get some data from table and print or use only the data.
Failing to do both ...
$con=mysqli_connect($MySQL_Host,$MySQL_User,$MySQL_Password,$MySQL_DB);
function echo_func_test ($db_con,$INPUT_IndustryName){
$result = mysqli_query($db_con,"SELECT ID FROM Industries where IndustryName = '$INPUT_IndustryName'");
//$Data = mysql_fetch_row($result);
var_dump ($result);
}
$IndustryName = Utilities;
echo_func_test($con,$IndustryName);
=================
the output is:
mysqli_result::__set_state(array(
'current_field' => NULL,
'field_count' => NULL,
'lengths' => NULL,
'num_rows' => NULL,
'type' => NULL,
)) root#virtual-dev:/home/ftpadmin/FirstPHP2# php Scripts/main.php
What am I doing wrong?

Categories