Show a text depending on the value of a column MySQL - php

I would like to know, how can I build a query that shows me an extra column where instead of showing the true value of the original column it shows me the text that i want?
For example, The Column, in this case "inputOutput", save values ​​0 or 1, and I would like to show in a query that if the value is 0 I can see the text "Output" and if it is 1 show "Input" in all the rows.
what function or structure could you use?
This is just an example of the columns that I would like to show:
Select idInventory, date, "text that that replaces 0 or 1 from inputOtput column"
from inventory;

You can use a case expression for this:
select
idInventory,
date,
case inputOutput
when 0 then 'Output'
when 1 then 'Input'
else '???'
end status
from inventory;

Related

wpdb->insert in ANY table always in third column inserts 0 instead of submitted string. Same string is inserted OK if submitted to another column

I have three tables defined where always
column 3 is "query", varchar(255) null
column 4 is "tag", varchar(255) null
both columns are the same charset
I submit string $s to column 3 via $wpdb->insert which should do sanitization (but adding extra sanitization to col 3 and col 4 or not adding it does not change anything at all)
function abc($a=null,$tag=null) {
global $wpdb;
$data = array(
'timestamp' => time(),
'query' => sanitize_text_field($a),
'tag' => sanitize_text_field($tag)
);
$format = array('%s','%d');
$wpdb->insert(SS_STATS,$data,$format);
return $wpdb->insert_id;
}
in the template I tried:
abc($s,$s);
abc("$s","$s");
abc("plz save this","$s");
abc("plz save this","plz save this: $s");
In each and every case, column 3 in the db recods 0. In each and every case, column 4 records the correct value just as it is submitted.
Why?
I tried:
changing the name of the column (maybe query is protected)
adding extra sanitization
not adding any sanitization
changing data type to text
changing data type to blob
For every attempt I drop the db and let it recreate.
No change in any case.
How can I save the string in column 3?
The problem was in the format:
$format = array('%s','%d');
$wpdb->insert(SS_STATS,$data,$format);
Removing the formatting solved the issue.
$wpdb->insert(SS_STATS,$data);

how to insert an array value like 'hello, world" in a different row in a table in in laravel model

how to insert an array value like 'hello,world" in a different row in a table in php laravel model,
the word hello,world I want to insert in a table named 'test_table', and its stores in an array variable,
I want to insert the word 'hello' in first row and the word 'world' insert into the second row.
In first : "hello,world" is a string
If you want a array variable : ["hello", "world"]
If you want transform your string into array you need to use :
explode function => Link
After you have got a model who test_table is linked and normally into your controller you need follow this example :
$testTable = new TestTable();
$testTable->myArray = ['hello', 'world'];
$testTable->save();

How to insert boolean value "false" as 0 with DB insert?

I receive data from a API request and it contains a value "true" or "false".
Before I've used PDO::PARAM_BOOL to bind the value and insert it into MySQL
Now I'm using Laravel and I want to know how I can insert this value as boolean value 1 or 0.
I've created a migration which contains
$table->boolean('businessorder');
I save the data in an array and insert it in my table:
$orderarray = [];
foreach($csv as $data => $orderdata){
$orderarray[] = [
...
'businessorder' => $orderdata['is-business-order'],
...
];
}
I receive the following error
SQLSTATE[22007]: Invalid datetime format: 1366 Incorrect integer value: 'false' for column 1
The error message also shows the data to insert in the database as "false" not as 0.
How can I make sure it will insert 0 or 1 instead of true or false
When I do
'businessorder' => (bool)$orderdata['is-business-order'],
It will set everything to 1 (because (bool)'false' is true)
When you try to put the value of $orderdata['is-business-order'] into the DB column, MySQL tries to put true or false.
But true or false cannot be stored in a boolean type column. So we can do something like the below code to get 1 or 0 instead of true or false.
businessorder' => ($orderdata['is-business-order']=='true'?1:0)
According to this post you could do something like this:
'businessorder' => filter_var($orderdata['is-business-order'], FILTER_VALIDATE_BOOLEAN),

How to check if the value is number or string for a field with type 'character varying' in php?

I am showing the list of 'locations' from the table 'Products'. Now this field 'locations' contains either a string value or a number. If it's a number, it refers to the id of the table 'free_shipping_locations' while if it's a string it refers to the location field of 'default_locations'. Now I have to display the 'locations' from the 'Products' table, but it should show the respective values, like if its a number it should show the location from 'free_shipping_location', if its a string than it should display that value. SO how can I check if the value of the field 'locations' is number or string. For Information: the datatype of 'locations' is character varying.
First, if you have any chance to change this database structure, do so. Having a single column with two completely different meanings will carry on making your life difficult for the lifetime of the application. Either put all the possible values in the referenced table and always store a location_id, or have two nullable columns, one location_id and the other location_free_text, with a constraint that every row has one or the other (and not both).
However, assuming you can't do that, the function you want in PHP is ctype_digit:
Checks if all of the characters in the provided string, text, are numerical.
So it will return true for '123', but false for 'abc' or '-1.2'.
Note that it will error if given something other than a string, so often you will want to "cast" the argument with (string) to ensure it is:
if ( ctype_digit((string)$your_value) ) {
// It looks like an ID
} else {
// It's some other string
}
I had to look up character varying in postgresql and my presumption is that PHP will ALWAYS receive a string from these column types so the solution is:
// Check if the string only contains digits
if( ctype_digit( $row[ 'locations' ] ) )
{
// we have an ID!
}
else
{
// we have some string which is probably not an ID!
}

PostgreSQL Update Issue With Quotation('')

I'm using PostgreSQL & Codeigniter. There is a table called folio in the database. It has few columns containing remarks1, remarks2, remarks3 as well. Data for the all the other columns are inserted when the INSERT statement executes for the 1st time.
When I try to execute below UPDATE statement later for the below 3 columns, remarks1 column get updated correctly. But remarks2, remarks3 columns are updated with ''.
UPDATE "folio" SET "remarks1" = 'test remark', "remarks2" = '', "remarks3" = '' WHERE "id" = '51';
Given that remarks1, remarks2, remarks3 columns data type is character varying. I'm using Codeigniter active records. At a time all 3 columns could be updated else single column could be updated depending on the user input.
What could be the issue? How can I fix this? Why columns are updated with ''?
As requested the php array in CI would be below
$data = array(
'remark1' => $this->input->post('remark1'),
'remark2' => $this->input->post('remark1'),
'remark3' => $this->input->post('remark1')
);
Function which saves the data contains below two lines only
$this->db->where('id', $folio_id);
$this->db->update('folio', $data);
Those columns are updated with '' because you tell them to?
Let's take a closer look at the query
UPDATE "folio"
SET
"remarks1" = 'test remark',
"remarks2" = '',
"remarks3" = ''
WHERE
"id" = '51';
First you select the table folio for the update.
Then you tell it to update remarks1 through remarks3 with new values. For remarks2 and remarks3 you specify to set them to an empty string. And that's what's going to happen.
Last but not least, you tell it to only apply this update to rows where id equals 51.
So, in order to only update remarks1 you can simply remove the other columns from your update:
UPDATE "folio"
SET
"remarks1" = 'test remark'
WHERE
"id" = '51';
Update:
I'm by far not a CI expert, but from what I see, I'd change the $data array to only contain information for remark1:
$data = array(
'remark1' => $this->input->post('remark1')
);
And (from my understanding) it should only update this single column.

Categories