Return BLOB column Which is Empty Or Not Empty as 1 or 0 - php

I have a table with BLOB column that some row has BLOB, some empty.
1 Apple BLOB-8KiB
2 Banana
3 Pear BLOB-6KiB
4 Orange BLOB-7KiB
Is there any way I can use PHP MYSQL to get the array like this:
$fruit = array(
array("1",Apple,1),
array("2",Banana,0),
array("3",Pear,1),
array("4",Orange,1)
);
I just want to change the BLOB data with 1, Empty with 0 in my PHP array. Pls help.

Your select statement can use IF and ISNULL (note these are not widely implemented in the same format on different database backends, this is for MySQL).
So you would use:
SELECT ID, Name, IF(ISNULL(BlobField), 0, 1) FROM TableName
IF allows you to choose one of two values according to a logical operation.
ISNULL returns true or false according to whether or not the value is NULL

Related

SQL: INSERT 1 if value=0 and 0 if value=1

I have data from an SQL query that is 'fileUnavailable' and need to insert into another table that is 'fileAvailable.' The values are either 1 or 0 and I need to do an SQL insert. Since fileUnavaiable and fileAvailable are opposite, I'd need to change the first value to either 1 if it's 0 or 0 if it's 1. Originally, I was thinking to just do an if, else statement to change the value, but that seems bulky and seems too simple for there not to be a method already.
I'm mostly curious as to whether or not SQL has something like !(fileUnavailable) but works for 1 and 0 because these values are ints in my db.
Pseudo:
INSERT INTO table (fileAvailable) VALUES ( NOT($fileUnavailable));
Use a hint with ABS:
UPDATE table SET field = ABS(field - 1)
So if field is 1, then field - 1 is 0, abs(0) is still 0.
And if field is 0, then field - 1 is -1, abs(-1) is 1.
ABS() man page.
For query you provided in a question:
INSERT INTO table (fileAvailable) VALUES ( ABS($fileUnavailable - 1) );
I found this from another stack overflow question.
INSERT INTO `table` SET `my_bool` = NOT my_bool
Update a boolean to its opposite in SQL without using a SELECT

PHP: Postgres Array and pg_insert

I know I can insert an array into a Postgres database with pg_query.
INSERT into table (field) VALUES ('{{element, element},{}}')
But how about using pg_insert?
$array["field"] = "{{element, element},{}}";
$result = pg_insert($con, "table", $array);
Will the string {{element, element},{}} be actually inserted into field as a 2D Postgres array?
I've always wanted to test that out but currently I don't have any PostgreSQL DB to test with..
I just ran your specific example.
(1) in Postgres:
CREATE TABLE atable (afield text[][]);
(2) in PHP:
$array["afield"] = "{{'element', 'element'},{}}";
$result = pg_insert($this->conn, "atable", $array);
And I got the following error:
Notice: pg_insert(): Unknown or system data type '_text' for 'afield' in ...
I tried playing around with the array value: make it 2x2 array, one-dimensional, etc., etc. - the same result. I even changed the table to have the field as one-dimensional array: text[] and changed the code accordingly - and I still get the same result.
I started digging further and found the following on PHP documentation for pg_insert:
This function is EXPERIMENTAL. The behaviour of this function, its
name, and surrounding documentation may change without notice in a
future release of PHP. This function should be used at your own risk.
Basically, it's pretty buggy and shouldn't be used. Interestingly, using
pg_query("INSERT INTO...")
works just fine. Hopefully, this answers your question. :)
As many dimensions as you like, but two is fine.
=> CREATE SCHEMA ztest;
CREATE SCHEMA
=> CREATE TABLE tt (a int[3][3]);
CREATE TABLE
=> INSERT INTO tt VALUES (ARRAY[ARRAY[1,2,3], ARRAY[4,5,6], ARRAY[7,8,9]]), (ARRAY[ARRAY[11,12,13],ARRAY[14,15,16],ARRAY[17,18,19]]);
INSERT 0 2
=> SELECT * FROM tt;
a
------------------------------------
{{1,2,3},{4,5,6},{7,8,9}}
{{11,12,13},{14,15,16},{17,18,19}}
(2 rows)
=> INSERT INTO tt VALUES ('{{21,22,23},{24,25,26},{27,28,29}}');
INSERT 0 1
=> SELECT * FROM tt;
a
------------------------------------
{{1,2,3},{4,5,6},{7,8,9}}
{{11,12,13},{14,15,16},{17,18,19}}
{{21,22,23},{24,25,26},{27,28,29}}
(3 rows)
=> SELECT a[2][3] FROM tt;
a
----
6
16
26
(3 rows)

PHP fetch MySQL array with an index

When fetching an array from MySQL the rows are typically returned with a key from 0 to the size of your recordset:
row[0][key][value]
Is it possible to have one of the fields from the select statement returned as the key in the array?
For example. Assuming my data set has StudentID, Name, City, etc.
How can I select into an array where I could refer to the StudentID as the index like this:
rows[StudentID][Name]
rows[StudentID][City]
etc.
Thanks!
PDOStatement::fetchAll
To return an associative array grouped by the values of a specified column, bitwise-OR PDO::FETCH_COLUMN with PDO::FETCH_GROUP.
// Other PDO stuff to get a statement - abstract below
$result = PDOStatement::fetchAll( PDO::FETCH_COLUMN | PDO::FETCH_GROUP, 0 );
See example 3 on this page
Depending on which library you are using:
mysql_fetch_assoc()
mysqli_fetch_assoc()
PDO fetches both by default.

Floats are not saved correctly into SQLite table using PHP

I'm using SQLite 3 with PHP.
The SQLite table consists of 2 INTEGER and one FLOAT column. When saving data into this table using PHP floats are not saved correctly (default value is stored instead). The two integer columns are saved. Any ideas what could be wrong? Thank you.
Simplified code that actually works correctly:
$conn = new SQLite3('dbFileName');
$conn->query("CREATE TABLE data (
id INTEGER NOT NULL DEFAULT 0 ,
ts INTEGER NOT NULL DEFAULT 0 ,
value FLOAT NOT NULL DEFAULT 0
);"
);
$conn->query("REPLACE INTO data(id,ts,value) VALUES ('1','1234567890','12.1')");
-> 1|1234567890|0
This is just a suggestion, seeing as I have never used SQLite, but are you sure the numbers should be quoted? That seems somewhat odd to me.
Try:
$conn->query("REPLACE INTO data(id,ts,value) VALUES (1, 1234567890, 12.1)");
Sqlite data types are typeless, so your queries have to work:
all the following queries works for me
REPLACE INTO data(id,ts,value) VALUES ('1','1234567890','12.1');
REPLACE INTO data(id,ts,value) VALUES ('1','1234567890','12,123')
REPLACE INTO data(id,ts,value) VALUES ('1','1234567890','abc');
Check your PHP variable, you might have a bug and you pass a null variable.

Directly fetching a list from a column's data with MySQL

Let us suppose I have to deal with lots of grandmothers that have lots of cats. I have a table granny_cat :
granny_id | kitty_name
--------------------
1 Kizzy
1 Jimmy
1 Katty
2 Georges
2 Albert
3 Panther
and I want to retrieve a list of granny 1 cat's, i.e. to get something like (with php syntax)
array("Kizzy","Jimmy","Katty")
However, the query
SELECT kitty_name WHERE granny_id = 1
returns something like
array
(
array('kitty_name' => "Kizzy"),
array('kitty_name' => "Jimmy"),
array('kitty_name' => "Katty")
)
what is quite logical, as I can fetch two, or more fields with a similar query. I can obviously map this array to get what I want, however, I wonder whether there is a (simple) way to get it directly from mysql, or not.
Thanks.
SELECT GROUP_CONCAT(kitty_name)
FROM mytable
WHERE granny_id = 1
will give you the comma-delimited list which you can explode into an array.
MySQL does not support native array datatype. In PostgreSQL you would be able to do the following:
SELECT ARRAY
(
SELECT kitty_name
FROM mytable
WHERE granny_id = 1
) AS kitties
, which would give you a native PHP array.
A wrapper library will typically handle this, e.g. ADODb has the GetCol() method:
$names=$db->GetCol('SELECT kitty_name WHERE granny_id = 1');

Categories