laravel how to use query builder DB::table(..) with DB::connection() - php

I have 2 databases defined in config/databases, mysql and mysql2
The default connection is mysql
I need to get this data from mysql2
$programs=DB::table('node')->where('type', 'Programs')->get();
The docs tell me I can change the connection using
$programs=DB::connection('mysql2')->select(...)
which would let me run a sql statement to get an array for $programs.
But I am wondering if there is a way to combine the 2 statements i.e. use query builder on specific db::connection.

You should use:
$programs=DB::connection('mysql2')->table('node')->where('type', 'Programs')->get();

Related

How to pass two connections to mysqli_query for running SELECT INTO query with two different server databases

My query is that I need to run a SELECT INTO query from One Server Database Table to Another Server Database Table. For that I am having two different mysqli_connect() connections so how can I run this query through mysqli_query as far as I know we can pass only one connection to it through arguments. Or is there any other way out to perform the same.
You can first create one connection of the first database through mysqli_query() and then take that output and store it into temp variable and then create another second server connection and then manipulate your data!

how to prepare a query to json postgresql field in php

I am having a problem creating a query in PHP to retrieve a json value from a postgresql database.
The problem is within the json WHERE clause of the following simple query
select * from json_data where jsonfield ? 'roottag';
if i use the PDO prepare:
$query->prepare($sqlSelect);
PDO will translate the ? (postgres json operator to see if the json field starts with a specific tag) to $1.
How do i write a sql statement similar to the one above to be used within php PDO?
To safe other people some time: A PATCH IS ON THE WAY! (at the time of writing)
see: https://github.com/php/php-src/pull/1967

Make eloquent give only sql clause

I was trying to rip off every connection function of Laravel's eloquent to give me SQL clause without querying it with PDO ( I also cleaning off every PDO instances from the code) and make eloquent a standalone package. I just want it to give me sql clause without connect to any db when I use an eloquent model just like this :
Request : Users::with('group')->take(5)->get();
Response : Select * from users.....
Can this be done ?
use ->toSql() instead of ->get()

How to log MySQLi querys in a new table

Using PHP5, with the MySQLi class for executing MySQL queries, how can I log each query processed using MySQLi::query in a new table in the currently connected database?
Is it possible to do some extension on the function, like in Java?
If you use version of mysql that supports triggers. Just create a trigger, this way you will not have to pollute code with unnecessary business logic
http://dev.mysql.com/doc/refman/5.0/en/triggers.html
Support for triggers is included beginning with MySQL 5.0.2. So when you deploy your app create triggers at the same time you create tables.
Define a table for your logs
create table querylog (
tm timestamp,
query varchar(4096));
and when you do a query, log the query string in this table
insert into querylog (tm, query)
values(now(), '$query_string')
You can do this either explicit at every query or in a function my_query, you define and call instead of mysqli_query or, as #E_p suggested, in a trigger.

how to use sql connection in PHP to execute a query on two databases

how can i run a query that joins two tables from TWO different Databases in mssql_query or mysql_query in php
for example
$conn=mssql_connect($ip,$username,$password);
mssql_select_db("DB1",$conn);
$q="select A.name,B.ID from DB1.dbo.T1 A, DB2.dbo.T2 B where A.ID=B.ID";
$res=mssql_query($q);
how to run such query??
Just prefix the tablenames with the database name, as you are already doing.
The user login that you are using to connect to mySQL needs to have access to both databases. Without this, it is impossible.
I think something like this:
SELECT X.field1, Y.field2
FROM database1.table_a AS X
INNER JOIN database2.table_b as Y
ON X.id=Y.id
[EDITED]
Sorry I didn't finish the post, you should use mysqli http://www.php.net/manual/en/mysqli.query.php (don't worry for the constructor, put just 1 database) and run the query as a regular query. Also, like the guy in the top said, the user that makes the query must have the permissions for both tables.

Categories