Specify columns for PDO::FETCH_KEY_PAIR - php

I have a table with a following columns (more than 2) in my database:
1. id
2. name
3. email
I am trying to fetch key value pairs using PDO by fetching the fetchMode to PDO::FETCH_KEY_PAIR and specifying columns id and name which works just fine
But when i try to specify name and email as the desired columns i get the result set but the key is not name but usual numeric zero based indexes
0 => some#email.com
Why?
BTW i am using laravel, here is my code:
$users = new User;
$users->getConnection()->setFetchMode(PDO::FETCH_KEY_PAIR);
return $users::get(array('name','email'));

I was able to get the desired result by using:
User::lists('email', 'name');

Related

How to retrieve data based on json column matching with special data

delivery_charges table has a json type column named locations. this column has the following data
[{"district_id":"1"},{"district_id":"2"},{"district_id":"3"}]
But $deliveryCharges is always an empty array when I run this code:
$deliveryCharges = DB::table('delivery_charges')
->whereJsonContains('locations->district_id',post('district_id'))
->get();
dump(post('district_id')); // outputs 1
dump($deliveryCharges); // outputs []
I can see value of post('district_id') is 1. And when I comment whereJsonContains() lines it return the data. So it seems to me there is a problem in my where clause.
If your column is named location, in the where clause you are named locations. Just use the correct column name:
$query->whereJsonContains('location->district_id',post('district_id'));
Try this
$deliveryCharges = DB::table('delivery_charges')
->where('locations','LIKE','%"district_id":"'.post('district_id') .'"%')
->get();

How to get a column's default value using Laravel?

I am building a custom artisan command that needs to be able to access the database's default values for certain columns. I cannot use the attributes array. So instead I need another way.
I have tried to use Schema. I have been able to get the table DB::table($table) and the column names Schema::getColumnListings($table) but not the default values.
Is there some other way to get the default values?
The Laravel Schema Builder only returns column names by design. But you can use the same approach Laravel uses internally by executing a database statement:
$results = DB::select('
select column_default
from information_schema.columns
where
table_schema = ?
and table_name = ?
', [$database, $table]);
// Flatten the results to get an array of the default values
$defaults = collect($results)->pluck('column_default'))
The above example works for a MySQL database, but you can see the approaches for other databases in the Illuminate\Database\Schema\Grammars namespace of the Laravel source code by searching for the method compileColumnListing.
In Laravel 5+ (including 6 and 7), you can get the db table column metadata (ie type, default value etc) in the following way:
use Illuminate\Support\Facades\Schema;
For all columns:
$columns = Schema::getConnection()->getDoctrineSchemaManager()->listTableColumns('table_name');
For a single column:
$column = Schema::getConnection()->getDoctrineColumn('table_name'', 'column_name');
getDoctrineSchemaManager method returns a array of \Doctrine\DBAL\Schema\Column Class Instances. By using this you can get everything about a db table column.
getDoctrineColumn method returns the instance of \Doctrine\DBAL\Schema\Column class.
Couple of methods from \Doctrine\DBAL\Schema\Column class:
$column->getName();
$column->getNotnull(); // returns true/false
$column->getDefault();
$column->getType();
$column->getLength();

mysql_query inject - replace returned field

im trying to learn and understand mysql inject, i have created demo case.
SELECT ret_variable FROM data WHERE name = '".$name."' AND age = ".$age;
then if(ret_variable == 2){something} but query originally returns 1 and i need to force it to output 2
How to modify $age variable to set custom output field for ret_variable(only in response) ?
I have tried few ways with OR but didn't wroked.
I see no practical application other than learning. I assume since you know the code , you have permission to test this out. So let's give it a go!
You can only return a 2 for the ret_variable when there is a row in the database with a value of 2 as the ret_variable and you know the name value of that row. You can for instance enter that name and the following to bypass the correct value for the age.
age AND ret_value = 2
That would create the following query:
SELECT ret_variable FROM data WHERE name = 'John' AND age = age AND ret_value = 2;
The principle of mysql injection is this sort of manipulation of the query. But you can not force a value which is returned unless there is a row in the database with this value for ret_variable and you can somehow select this row.
When you don't know the name (or there is no record of your known name with a ret_variable of 2) it is not possible.
Since the AND operator has precedence over the OR operator you cannot manipulate the query to give a 2 as ret_variable. This is because the name = '?' part will always fail.

MySQL query to return json name value

I'm working with a table in which information is stored in a table in JSON format. The JSON value field looks like:
select * from k2_extra_fields where id = 2 and published = 1;
id | value
2,[{"name":"Apples","value":1,"target":null,"alias":"","required":0,"showNull":1},{"name":"Pears","value":2,"target":null,"alias":"","required":0,"showNull":1},{"name":"Mangos","value":3,"target":null,"alias":"","required":0,"showNull":1},{"name":"Guava","value":4,"target":null,"alias":"Fruit","required":0,"showNull":1},{"name":"Pineapple","value":5,"target":null,"alias":"Fruit","required":0,"showNull":1}]
Or values in a simple line by line view (minus the ID):
[
{"name":"Apples","value":1,"target":null,"alias":"","required":0,"showNull":1},
{"name":"Pears","value":2,"target":null,"alias":"","required":0,"showNull":1},
{"name":"Mangos","value":3,"target":null,"alias":"","required":0,"showNull":1},
{"name":"Guava","value":4,"target":null,"alias":"Fruit","required":0,"showNull":1},
{"name":"Pineapple","value":5,"target":null,"alias":"Fruit","required":0,"showNull":1}
]
The query that leads me here returns the value of 3. 3 = Mangos. How do I take the '3' value and match it up with the stored names/values so that I end up with the output, Mangos?
It should be possible with build in mysql functionality, but very hard and 'not clever' idea to do. If you really need to compute this problem within mysql, you would need to actually add new funtionality to your mysql. Look up on UDF plugins: http://dev.mysql.com/doc/refman/5.1/en/udf-compiling.html

php SimpleCassie Cassandra TimeUUID

I created a column family Users using following command:
create column family Users with comparator=TimeUUIDType and default_validation_class=UTF8Type;
Then I insert a column into Users. It shows as follows.
RowKey: jsmith
=>(column=66829930-515b-11e0-8443-0f82b246fa40, value=hello, timestamp=1300451382)
I want to access it using SimpleCassie. The command is:
$data = $cassie->keyspace('Keyspace1')->cf('Users')->key('jsmith')->column('66829930-515b-11e0-8443-0f82b246fa40')->value();
(I also tried: $data = $cassie->keyspace('Keyspace1')->cf('Users')->key('jsmith')->column($cassie->uuid('66829930-515b-11e0-8443-0f82b246fa40')->__toString())->value();)
However, They do not work. It always return NULL.
How can I get the column value (hello) I want?
Try:
$cassie->keyspace('Keyspace1')->cf('Users')->key('jsmith')->column($cassie->uuid('66829930-515b-11e0-8443-0f82b246fa40')->uuid)->value();)
Cassandra expects the binary representation of a UUID, not a hex/string representation.

Categories